我无法理解为什么这段代码会出现分段错误
i'm unable to understand why this block of code is giving segmentation fault
我试图实现以下功能。它的作用是:
- 接受坐标
- 为周围的单元格分配权重。
- 将这些权重和单元格方向存储在地图中。
- 在地图中循环。
- 调用移动与邻居单元坐标。
代码片段:
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 }
向后追踪信息:
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
我的实现出了什么问题?
下面是代码和valgrind日志的链接,如果需要的话:https://www.dropbox.com/s/5m8zfxubq6lcl8o/terrainExample.cpp?dl=0https://www.dropbox.com/s/wq7ob1uevwutsov/logfile.out?dl=0
在这段代码中,我使用vector代替map
这是一段有趣的代码,我看得很开心。让我们来剖析一下。
首先,valgrind抱怨说:
==15718== Warning: client switching stacks? SP change: 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)
。我只是删除了参数的一致性,你会在一分钟内看到为什么。 -
在函数的开头添加一个标签:
void nextMove(int x, int y) { again:
-
不要在
nextMove(nx, ny);
的末尾递归地调用函数,这样做:x = nx; y = ny; goto again;
有些人会讨厌我用goto
,所以我会让他们提供一个没有goto
的答案。
希望这对你有帮助!
相关文章:
- 将鼠标悬停在问题上时与预期">"相关的代码错误
- VI工作室代码错误无法打开输出文件主.exe
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- 有关矩阵的代码错误导致分段错误(内核转储)
- C++ Schannel POST 400 错误代码错误请求
- Java 本机访问代码错误:"Invalid memory access"
- 代码错误修改
- C++代码错误分配
- "_FCbuild"不能用作函数 - C/C++ 代码错误
- 我的代码错误类型警告 1 警告 C4018 和更多错误
- 代码错误(从 1 到 100 找到质数)
- OpenCV VisualStudio,C 代码错误尝试访问像素
- 当我使用的教程完全没有错误时,为什么我的代码错误
- 简单的NOOB C 输入/输出问题:代码错误
- 为什么enumProcessModules返回false值和299代码错误
- WinhttpSetoption()失败设置TLSV1.2,带有错误代码错误_internet_incorrect_ha
- 苹果安全传输代码错误
- C++ - 国际象棋主教移动代码错误
- 导入Python库时,嵌入了Python代码错误
- 编译提升C 代码错误