c++-程序运行跳出来这个 什么问题??

问题描述

程序运行跳出来这个 什么问题??

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看看,能否先找到出错的函数调用。

解决方案五:

这个是段错误,一般是指针指向了不该指向的地方

时间: 2024-09-09 04:45:35

c++-程序运行跳出来这个 什么问题??的相关文章

c++-[紧急求救]C++:在循环结构中使用链表,程序运行终端

问题描述 [紧急求救]C++:在循环结构中使用链表,程序运行终端 如题.(这是图像处理中的中值滤波,不过问题不涉及图像处理)链表操作都没有问题,在另外的程序中测试过.这这段代码中第一次调用也没有问题,就是第二次到list.insert()时会跳出中断:这段代码如下: int i j x y p t;//p为当前像素位置 int a[arg*arg] = {0}; linklist list; for (y = 0; y<nHeight - arg + 1; y++) { for (x = 0;

c++程序运行时出现string subscript out of range

问题描述 c++程序运行时出现string subscript out of range int main(){ vector a; string s; while(getline(cin,s)){ a=spite(s); for(vector<string>::size_type i=0;i!=a.size();i++) cout << a[i] << endl; } return 0; } vector spite([如果在这里加上const则可以正常运行]stri

c语言-每次程序运行到这一部都会卡住没反应,估计是tcount部分的代码有问题,望高手指点

问题描述 每次程序运行到这一部都会卡住没反应,估计是tcount部分的代码有问题,望高手指点 #include #include #include #include #include "head.h" void tcount(cour *head); void mcount(cour *head); void count(cour *head) { int i; while(1) { printf("tt请选择下一步操作:n"); printf("tt1.

计时器-flash程序运行了几秒后似乎程序被直接重置了

问题描述 flash程序运行了几秒后似乎程序被直接重置了 import flash.utils.Timer;import flash.events.TimerEvent;var timer:Timer = new Timer(100); //100毫秒一跳var i:Number = 0;var flag:Number = 0;bangle.y=100;bangle.x=100; background.stop();timer.addEventListener(TimerEvent.TIMERr

这个程序运行一下为什么提示exe已停止工作?

问题描述 这个程序运行一下为什么提示exe已停止工作? #include void strcpy(char *,char *,int); int main() { char *s = "1245"; char *t = "3123"; strcpy(s,t,3); printf("%sn",s); return 0; } //函数strcpy(s,t,n)将t中UI多前n个字符复试到s中 void strcpy(char *s,char *t,i

c-关于Python程序运行,一个算法文档不知怎么运行!求助

问题描述 关于Python程序运行,一个算法文档不知怎么运行!求助 网上下了这么个程序,是个Rabin指纹算法的代码,但是不知道怎么跑这个程序.请大神指点:这个文件包含这些内容然后src总包含:test中包含的内容如下:因为里面既有.py文件,也有.c文件~不知道怎样才能正确运行~求大神告知,谢谢 解决方案 直接执行setup.py 前提是你的机子中安装了python 解决方案二: 先看README.md的说明,应该是直接安装setup.py,它会编译c等生成库 解决方案三: 这是一个modul

spring-maven 打jar包,引发程序运行时候报错

问题描述 maven 打jar包,引发程序运行时候报错 java项目A,用maven 打完jar包之后,放入项目B中使用,运行报如下错误java.lang.NoSuchFieldError: ALIAS_TO_ENTITY_MAP: 用MyEclipse 自带的export jar打包,却没有这个问题,很是郁闷,求解... 报错代码:query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); 解决方案 请检查确认项目 B 里的 A.

iostream-为什么我这个c++程序运行后直接关掉了窗口,就是应该运行出来了,但是还没来得及看结果就被自动关了

问题描述 为什么我这个c++程序运行后直接关掉了窗口,就是应该运行出来了,但是还没来得及看结果就被自动关了 程序代码如下: #include "stdafx.h" #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { double daphne = 100.0; double cleo = 100.0; const double rate_1 = 0.1; const double r

【求助】如何修改程序运行中的数据并保存

问题描述 [求助]如何修改程序运行中的数据并保存 使用C/C++实现程序在执行时可以变更原始数据并保存,关闭以后再次执行该程序时,程序使用更改后的数据运行.(不是将修改后的数据保存在配置文件.或注册表里面.保存在原有的程序中) 解决方案 你这需求,我觉得不靠谱.一般来说都是存在加密文件的.比如xxx.db这个格式你可以自定义,但加密方法只有你知道,提取时解密即可