24小时热门版块排行榜    

查看: 255  |  回复: 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 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 石河子大学(211、双一流)硕博研究生长期招生公告 +3 李子目 2026-03-22 3/150 2026-03-22 21:01 by 怎么释怀
[考研] 311求调剂 +6 冬十三 2026-03-18 6/300 2026-03-22 20:18 by edmund7
[考研] 一志愿华中农业071010,总分320求调剂 +5 困困困困坤坤 2026-03-20 6/300 2026-03-22 17:41 by hxsm
[考研] 求调剂 +6 十三加油 2026-03-21 6/300 2026-03-22 17:00 by i_cooler
[考研] 308求调剂 +3 墨墨漠 2026-03-21 3/150 2026-03-22 16:54 by i_cooler
[考研] 293求调剂 +12 zjl的号 2026-03-16 17/850 2026-03-22 16:51 by i_cooler
[考研] 求调剂 +5 Zhangbod 2026-03-21 7/350 2026-03-22 13:13 by Zhangbod
[考研] 考研调剂 +4 来好运来来来 2026-03-21 4/200 2026-03-22 12:15 by 星空星月
[考研] 285求调剂 +6 ytter 2026-03-22 6/300 2026-03-22 12:09 by 星空星月
[考研] 286分人工智能专业请求调剂愿意跨考! +4 lemonzzn 2026-03-17 8/400 2026-03-21 22:49 by lemonzzn
[考研] 一志愿东华大学控制学硕320求调剂 +3 Grand777 2026-03-21 3/150 2026-03-21 19:23 by 简之-
[考研] 278求调剂 +9 烟火先于春 2026-03-17 9/450 2026-03-21 17:47 by 学员8dgXkO
[考研] 316求调剂 +6 梁茜雯 2026-03-19 6/300 2026-03-21 06:32 by Ecowxq666!
[考研] 机械专硕299求调剂至材料 +3 kkcoco25 2026-03-16 4/200 2026-03-21 03:52 by JourneyLucky
[考研] 303求调剂 +5 睿08 2026-03-17 7/350 2026-03-21 03:11 by JourneyLucky
[考研] 二本跨考郑大材料306英一数二 +3 z1z2z3879 2026-03-17 3/150 2026-03-21 02:29 by JourneyLucky
[考研] 288求调剂 +16 于海海海海 2026-03-19 16/800 2026-03-20 22:28 by JourneyLucky
[考研] 295材料求调剂,一志愿武汉理工085601专硕 +5 Charlieyq 2026-03-19 5/250 2026-03-20 20:35 by JourneyLucky
[考研] 求调剂 +3 暗涌afhb 2026-03-16 3/150 2026-03-20 00:28 by 河南大学校友
[考研] 085601专硕,总分342求调剂,地区不限 +5 share_joy 2026-03-16 5/250 2026-03-18 14:48 by haxia
信息提示
请填处理意见