Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ i';我无法理解这段代码为什么会出现分段错误_C++_Iterator - Fatal编程技术网

C++ i';我无法理解这段代码为什么会出现分段错误

C++ i';我无法理解这段代码为什么会出现分段错误,c++,iterator,C++,Iterator,我正在尝试实现以下功能。 它所做的是: 需要一个坐标 为周围的单元格指定权重 将这些权重和单元方向存储在地图中 在地图上循环。从最低重量开始 调用使用相邻单元格坐标再次移动 代码段: 120 void move(const int x, const int y) 121 { 122 map<int, int> nextDir; 123 map<int, int>::iterator it; 124 if((x == maxX

我正在尝试实现以下功能。 它所做的是:

  • 需要一个坐标
  • 为周围的单元格指定权重
  • 将这些权重和单元方向存储在地图中
  • 在地图上循环。从最低重量开始
  • 调用使用相邻单元格坐标再次移动
  • 代码段:

    120 void move(const int x, const int y)
    121 {
    122         map<int, int> nextDir;
    123         map<int, int>::iterator it;
    124         if((x == maxX - 1) && (y == maxY - 1))
    125         {
    126                 int groundCopy[maxX][maxY];
    127                 memcpy(((void *)&groundCopy), ((void *)&ground), sizeof(groundCopy));
    128                 traceBack(x, y);
    129                 memcpy(((void *)&ground), ((void *)&groundCopy), sizeof(ground));
    130                 printPPM();
    131         }
    132         for(int i = 0; i < 8; ++i)
    133         {
    134                 if(!isValid(x + dirX[i], y + dirY[i]))
    135                         continue;
    136                 int temp = weight[x][y][0] + ground[x + dirX[i]][y + dirY[i]] + disWeight(x, y, x + dirX[i], y + dirY[i]);
    137                 if(!(weight[x + dirX[i]][y + dirY[i]][0] == numeric_limits<int>::max()))
    138                         temp += weight[x + dirX[i]][y + dirY[i]][0];
    139                 if(temp < weight[x + dirX[i]][y + dirY[i]][0])
    140                 {
    141                         weight[x + dirX[i]][y + dirY[i]][0] = temp;
    142                         weight[x + dirX[i]][y + dirY[i]][1] = 7 - i;
    143                         nextDir[temp] = i;
    144                 }
    145                 else
    146                         continue;
    147         }
    148         for(it = nextDir.begin(); it != nextDir.end(); ++it)
    149                 move(x + dirX[it->second], y + dirY[it->second]);
    150 }
    
    120无效移动(常量整数x,常量整数y)
    121 {
    122 map NEXTIR;
    123 map::迭代器it;
    124如果((x==maxX-1)和&(y==maxY-1))
    125         {
    126 int地面拷贝[maxX][maxY];
    127 memcpy((作废*)和地面副本),(作废*)和地面副本),sizeof(地面副本);
    128回溯(x,y);
    129 memcpy((作废*)和地面)、((作废*)和地面副本)、sizeof(地面));
    130 ppm();
    131         }
    132表示(int i=0;i<8;++i)
    133         {
    134如果(!isValid(x+dirX[i],y+dirY[i]))
    135继续;
    136内部温度=重量[x][y][0]+地面[x+dirX[i][y+dirY[i]]+disWeight(x,y,x+dirX[i],y+dirY[i]);
    137如果(!(权重[x+dirX[i]][y+dirY[i]][0]==数值极限::max())
    138温度+=重量[x+dirX[i][y+dirY[i]][0];
    139如果(温度<重量[x+dirX[i]][y+dirY[i]][0])
    140                 {
    141重量[x+dirX[i]][y+dirY[i]][0]=温度;
    142重量[x+dirX[i]][y+dirY[i]][1]=7-i;
    143下一个温度=i;
    144                 }
    145其他
    146继续;
    147         }
    148 for(it=nextDir.begin();it!=nextDir.end();++it)
    149移动(x+dirX[它->秒],y+dirY[它->秒];
    150 }
    
    回溯信息:

    Program received signal SIGSEGV, Segmentation fault.
    0x0000000000401760 in move (x=<error reading variable: Cannot access memory at address 0x7fffff5ab18c>, y=<error reading variable: Cannot access memory at address 0x7fffff5ab188>) at codes/terrainExample.cpp:121
    121 {
    (gdb) bt
    #0  0x0000000000401760 in move (x=<error reading variable: Cannot access memory at address 0x7fffff5ab18c>, y=<error reading variable: Cannot access memory at address 0x7fffff5ab188>) at codes/terrainExample.cpp:121
    #1  0x0000000000401bfa in move (x=0, y=1) at codes/terrainExample.cpp:149
    #2  0x0000000000401bfa in move (x=0, y=0) at codes/terrainExample.cpp:149
    #3  0x0000000000401dbb in solve () at codes/terrainExample.cpp:167
    #4  0x0000000000401f1c in main () at codes/terrainExample.cpp:186
    
    程序接收信号SIGSEGV,分段故障。
    0x0000000000401760在代码/terrainExample.cpp:121处移动(x=,y=)
    121 {
    (gdb)英国电信
    #0 0x0000000000401760在代码/terrainExample.cpp:121处移动(x=,y=)
    #1 0x0000000000401bfa在代码/terrainExample处移动(x=0,y=1)。cpp:149
    #2 0x0000000000401bfa在代码/terrainExample处移动(x=0,y=0)。cpp:149
    #代码/terrainExample.cpp:167处的solve()中的3 0x0000000000401dbb
    #代码/terrainExample.cpp:186处的main()中的4 0x0000000000401F1F1C
    
    我的实现有什么问题

    以下是代码和valgrind日志的链接(如果需要):


    在这段代码中,我使用的是矢量而不是地图。

    这是一段有趣的代码,我非常喜欢看它。让我们来分析一下

    首先,valgrind抱怨说:

    ==15718==警告:客户端交换堆栈?SP更改:0xfff000420-->0xffed6b8d8

    这告诉我某个地方有一个堆栈崩溃。浏览代码后,确实有大量的局部变量存储在堆栈上,因此:

  • 尝试减小图像的大小(例如设置
    const int maxX=320;const int maxY=640;
    修复了堆栈问题)
  • 但如果您确实需要较大的图像大小,只需执行动态内存管理即可
  • 但是,应用程序仍然会在某个地方崩溃……我发现在递归中一遍又一遍地调用
    nextMove
    ,会再次干扰堆栈。因此,为了让事情变得更好,请执行以下操作:

  • nextMove
    void nextMove(const int x,const int y)
    更改为
    void nextMove(int x,int y)
    。我刚刚删除了参数的constanness,您很快就会明白原因
  • 在函数的开头添加标签:

    void nextMove(整数x,整数y)
    {
    同样:

    最后但并非最不重要的是:

  • 不要在
    nextMove(nx,ny);
    末尾递归调用函数,而是执行类似的操作:
    x=nx;y=ny;再次转到;

  • 有些人会因为我使用了
    goto
    而讨厌我,所以我会要求他们提供一个没有任何
    goto
    的答案


    希望这会有所帮助!

    在使用调试器逐步检查代码时检查索引变量,并确保在边界中访问数组。第134行我正在这样做。bool isValid(const int x,const int y){if((x=maxY))返回false;返回true;}在debug中,我也检查了。谢谢@fritzone,它成功了。我更改了所有大型阵列以获得动态分配。没有使用goto,因为它会更改预设nextMove调用的x和y值。