问题描述
- 程序运行跳出来这个 什么问题??
-
it stopped with signal SIGSEGV,Segmentation fault
windows.h stdio.h stdlib.h stdio.h
-----------------------------------------------------以下正文int xnum;
int ynum;
int a[100][100];
#define X0 100
#define Y0 100
#define WIDTH 20
typedef struct
{
int x;
int y;
}NODE;
typedef struct
{
NODE nownode;
NODE prenode;
int dir;
}GRID;NODE start;
NODE end;
GRID grid;//-------------------------------顺序栈
class Stack
{
private:int size; public: NODE *p; int top; Stack(int n=0); ~Stack(); bool isEmpty(); bool isFull(); bool push(const NODE k); bool pop();
};
Stack::Stack(int n)
{
p=0;
if(n>0)
p=new NODE[n];
size=n;
top=-1;
}
Stack::~Stack(){
delete []p;
size=0;
top=-1;
}
bool Stack::isEmpty(){
return(top==-1);
}
bool Stack::isFull(){
return(top>=size-1);}
bool Stack::push(const NODE k){
if(!isFull()){
top++;
p[top]=k;
return true;
}
return false;
}
bool Stack::pop(){
if(!isEmpty()){
NODE k=p[top];
top--;
return 1;
}
return 0;
}//。-----------------------------画框
void Init()
{
FILE* fp;
fp=fopen("mice.txt","r");
fscanf(fp,"%d%d%d%d%d%d",&xnum,&ynum,&start.x,&start.y,&end.y,&end.x);//24 24 1 1 24 1
int i,j;
for(j=0;j<=ynum+1;j++)
for(i=0;i<=xnum+1;i++)
a[j][i]=1;
for(j=1;j<=ynum;j++)
for(i=1;i<=xnum;i++)
fscanf(fp,"%d",&a[j][i]);
fclose(fp);grid.nownode =start;//--------------初始位置 (1,1)
}
void reaction(Stack path,int x,int y){
grid.nownode.x=x;
grid.nownode.y=y;
path.push(grid.nownode);
//PrintNum(120,50,grid.nownode.x);
//PrintNum(140,50,grid.nownode.y);//DrawRectangle(X0+grid.nownode.y*WIDTH,Y0+grid.nownode.x*WIDTH,X0+(grid.nownode.y+1)*WIDTH,Y0+(grid.nownode.x+1)*WIDTH,MOUSECOLOR);
}
int MouseMove(Stack path)
{int x,y; x=grid.nownode.x; y=grid.nownode.y;//--------------现在位置 if (end.x==x && end.y==y){ grid.nownode.x=x; grid.nownode.y=y; path.push(grid.nownode); return 1;
}
else{ a[x][y]=1;//------------------------------------------------似乎指出的是这里 if(a[x][y+1]==0) reaction(path,x,y+1); else if(a[x+1][y]==0) reaction(path,x+1,y); else if(a[x][y-1]==0) reaction(path,x,y-1); else if(a[x-1][y]==0) reaction(path,x-1,y); else { path.pop(); grid.nownode.x=path.p[path.top].x; //上一个值作现在位置 grid.nownode.y=path.p[path.top].y; } } return 0;
}
int main(){
Init(); Stack path(2000); //---------------------//创建顺序表 path.push(start); int x,y; x=grid.nownode.x; y=grid.nownode.y;//--------------现在位置 while(end.x!=x || end.y!=y) MouseMove(path);
for(int i=0; i<=path.top;i++)
a[path.p[path.top].x][path.p[path.top].y]=2;for(int i=0;i<=xnum+1;i++)
{
for(int j=0;j<=ynum+1;j++)
printf("%d",a[i][j]);
printf("n");
}
scanf("%d",&x);
return 0;
}---------------------------------------------------mice.txt文件 (迷宫问题 C-FREE下)
24 24 1 1 24 1
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
解决方案
Linux系统的吧,可能是内存越界导致的。
用gdb单步跟踪一下吧,不然根本不知道蹦在哪里了。
也可以设置一下,崩溃会产生coredump文件,然后用gdb跟踪,应该很容易就找出问题了。
希望能够帮到你。
解决方案二:
段错误应该就是访问了不该访问的地址吧,在main函数开始设个断点,一步一步往下走,看看问题出在哪个函数里边,
如果进入循环里边,可以输出一下循环的控制变量之类的,看看是在哪一步出错,比如i==n的时候出的错,就可以加一句if (i==n)cout<<"a"<<endl;
之类的代码然后设断点到这里看看具体运行情况怎么回事。也可以直接在for循环语句这里设置断点情况。
解决方案三:
这也太长了,内存给小了吧?
解决方案四:
VS里Debug下打开call stack看看,能否先找到出错的函数调用。
解决方案五:
这个是段错误,一般是指针指向了不该指向的地方