24小时热门版块排行榜    

CyRhmU.jpeg
南方科技大学公共卫生及应急管理学院2026级博士研究生招生报考通知(长期有效)
查看: 224  |  回复: 0
当前主题已经存档。

zhliye

新虫 (初入文坛)

[交流] 【求助】求助高手(指针,栈)的问题,怎么总溢出啊???

用栈实现的一个迷宫算法,但是执行出错,自己没看出是啥问题,晕了,请大家帮忙指正。
#include
#include
#include

#define OK 1
#define STACKINITSIZE 100
#define STACKINCREMENT 50
#define OVERFLOW -2
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 10

//maze`s MAP ;'O' means can pass and '1' means here is blocked.
int Maze[MAXSIZE][MAXSIZE]={
       {1,1,1,1,1,1,1,1,1,1},
       {1,0,0,1,0,0,0,1,0,1},
       {1,0,0,1,0,0,0,1,0,1},
       {1,0,0,0,0,1,1,0,0,1},
       {1,0,1,1,1,0,0,0,0,1},
       {1,0,0,0,1,0,0,0,0,1},
       {1,0,1,0,0,0,1,0,0,1},
       {1,0,1,1,1,0,1,1,0,1},
       {1,1,0,0,0,0,0,0,0,1},
       {1,1,1,1,1,1,1,1,1,1},
  };

//dirArray ,from east->south->west->north,the coordinate increment
int Dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};

//PosType is way`s coordinate
typedef struct{
   int x;
   int y;
}PosType;

//current position whther can pass
int Pass(PosType curpos){
    if(Maze[curpos.x][curpos.y]==0) return OK;
    return ERROR;
}

//if this position can pass ,footprint
void FootPrint(PosType curpos){
     Maze[curpos.x][curpos.y]=2;
}

//if can't pass ,marking
void MarkPrint(PosType curpos){
     Maze[curpos.x][curpos.y]=4;
}

//seek the next position
PosType NextPos(PosType curpos,int dir){
   curpos.x=curpos.x+Dir[dir][0];
   curpos.y=curpos.y+Dir[dir][1];
   return curpos;
  }

//MazeType is stack`s element type
typedef struct{
int ord;
PosType seat;
int  di;
}MazeType;

//stack node
typedef struct
{
MazeType *base;
MazeType *top;
int stacksize;
}SqStack,*pStack;

//init stack
int InitStack (pStack S)
{
  S->base=(MazeType *)malloc(STACKINITSIZE * sizeof(MazeType));
  if(!S->base) exit(OVERFLOW);
   printf("init is succeed";
  S->stacksize=STACKINITSIZE;
  S->top=S->base;
return OK;
}

//Push Stack
int PushStack(pStack S,MazeType e){
if((S->top-S->base) >= S->stacksize)
    {
     S->base=(MazeType *)realloc(S->base,(S->stacksize+STACKINCREMENT) * sizeof(MazeType));
     if(!S->base) {printf("stack is full and reseting fails";exit(OVERFLOW);}
      S->top=S->base+S->stacksize;
      S->stacksize+=STACKINCREMENT;
    }
    *S->top++=e;
return OK;
}

//pop stack
int PopStack(pStack S,MazeType *q){
if(S->top==S->base) return ERROR;
  *q = *--S->top;
return OK;
}

//judge whther stack is empty
int  EmptyStack(pStack S){
    if(S->top==S->base) return OK;
    return ERROR;
}
//destroy stack
void DestroyStack(pStack S){
   S->top=S->base;
   free(S->base);
}
//above are stack`s base operation

//seek the mazepath
int   Mazepath(pStack S,PosType start,PosType end){
  PosType curpos=start;

  int curstep=1,dir=0;//dir is from 0 to3 coorespodence  east,south,west,north
  MazeType elem,pelem;

do{
     if(Pass(curpos)){
         FootPrint(curpos);
         elem.ord=curstep,elem.seat=curpos,elem.di=dir;
        if(PushStack(S,elem)) printf("push1 is wrong";
     if(curpos.x==end.x&&curpos.y==end.y) return TRUE;
     curpos=NextPos(curpos,elem.di);
     curstep++;
     }//if
    else{
        if(!EmptyStack(S)){
                  if( !PopStack(S,&pelem)) {printf("pop1 stack failing";exit(OVERFLOW);}//PopStack
                    while(!EmptyStack(S)&&pelem.di==3){
                        MarkPrint(pelem.seat);
                      if(!PopStack(S,&pelem)) printf("pop2 wrong";
                     }//while
              if(pelem.di<3){
                   pelem.di++;
                if(!PushStack(S,pelem)) printf("push2 is wrong";
                   curpos=NextPos(pelem.seat,pelem.di);
               }//if
        }//if
     }//else
  }while(!EmptyStack(S));
   return   FALSE;
}


main(){
        PosType  start,end,tests;
           start.x=1,start.y=1,end.x=8,end.y=8;
        SqStack test;
         MazeType elem,element;
         int i,j;
         printf("The Maze is; \n";
        for(i=0;i
          for(j=0;j
                printf("%d  ",Maze[j]);if(j==9) printf("\n";
            }
printf("%d",&test);
        if(Mazepath(&test,start,end)) printf("This maze has no exit!";
       printf("%d",&test);
    while(!EmptyStack(&test)){
           if(!PopStack(&test,&elem)) printf("mappath print fail";
           printf("%d->",elem.ord);
           printf("(%d,%d)->",elem.seat.x,elem.seat.y);
     }//while*/
DestroyStack(&test);
}


此程序运行*(GCC)后出现如下的错误,请高手指点

*** glibc detected *** ./a.out: realloc(): invalid pointer: 0xbff98759 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(realloc+0x39c)[0xb7e199ec]
/lib/tls/i686/cmov/libc.so.6(realloc+0x3c)[0xb7e1968c]
./a.out[0x80485f9]
./a.out[0x80487a0]
./a.out[0x80489f1]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7dc0450]
./a.out[0x8048441]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:01 461093     /home/lgp/C/a.out
08049000-0804a000 rw-p 00000000 08:01 461093     /home/lgp/C/a.out
0804a000-0806b000 rw-p 0804a000 00:00 0          [heap]
b7da9000-b7daa000 rw-p b7da9000 00:00 0
b7daa000-b7ef3000 r-xp 00000000 08:01 712708     /lib/tls/i686/cmov/libc-2.7.so
b7ef3000-b7ef4000 r--p 00149000 08:01 712708     /lib/tls/i686/cmov/libc-2.7.so
b7ef4000-b7ef6000 rw-p 0014a000 08:01 712708     /lib/tls/i686/cmov/libc-2.7.so
b7ef6000-b7ef9000 rw-p b7ef6000 00:00 0
b7ef9000-b7f03000 r-xp 00000000 08:01 696339     /lib/libgcc_s.so.1
b7f03000-b7f04000 rw-p 0000a000 08:01 696339     /lib/libgcc_s.so.1
b7f04000-b7f07000 rw-p b7f04000 00:00 0
b7f07000-b7f08000 r-xp b7f07000 00:00 0          [vdso]
b7f08000-b7f22000 r-xp 00000000 08:01 696332     /lib/ld-2.7.so
b7f22000-b7f24000 rw-p 00019000 08:01 696332     /lib/ld-2.7.so
bff84000-bff99000 rw-p bffeb000 00:00 0          [stack]
-1074166568Aborted
回复此楼
我要吃青菜。
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 zhliye 的主题更新
普通表情 高级回复(可上传附件)
信息提示
请填处理意见