不同地方的分段错误
我有一个相当大的程序(~1000 loc)。
当我运行该程序时,
./indexer
在第 800 次迭代时出现分段错误。
但是当我这样做时,
gdb indexer
run
它远远超出了第 800 次,并在第 1200 次迭代左右显示段错误。
我不明白为什么会这样。有什么指示可以指出我可能犯的错误吗?
编辑: 问题是这两种情况下可执行文件的不同行为可能是什么原因?
I have a pretty decent sized program (~1000 loc) .
When i run the program ,
./indexer
Gives segmentation fault on 800th iteration .
But when i do
gdb indexer
run
It goes well beyond 800th and shows seg fault at around 1200th iteration .
I dont understand why it is happening like this . Any pointers to possible mistake I am doing ?
EDIT:
The question is what might be reason for different behaviour of executable under the two conditions ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用调试器和不使用调试器运行时,程序的运行条件可能会有很大不同。如果您的程序中存在错误,尤其是未定义行为的错误,那么任何事情都可能发生。
由于使用调试器时问题仍然存在,因此我会先对其进行调试,然后如果在调试器下运行时问题得到解决,那么就完成了。否则你会有更多的工作要做。
The conditions under which your program runs can be very different when run with the debugger and without. If you have a bug in your program, especially a bug that is undefined behaviour, then anything could happen.
Since the problem still happens when using the debugger, I would debug that first, and then if the problem is solved when not running under the debugger, you're done. Otherwise you'll have a bit more work to do.
如果您使用的是类 Unix 系统(例如 Ubuntu),请使用以下命令安装 valgrind:
并运行它来检测与内存相关的错误:
如果您使用的是其他操作系统,请访问 valgrind 网站 并获取安装说明。
在 1k loc,我还建议您养成使用 googletest:当你的程序变大时,它会为你节省大量的调试时间。
If you are on an Unix-like system, like Ubuntu, install valgrind with:
and run it to detect memory-related errors:
if you are on some other OS, go to the valgrind website and get install directions.
At 1k loc, I also recommend that you get in the habit of unit-testing with something like googletest: it will save you a lot of debugging time as your program gets larger.
您正在读取或写入程序尚未分配的内存位置。然而,要准确了解这里发生了什么,您需要发布代码,最好是仍然存在段错误的小示例。
You are reading or writing to a memory location which has not been allocated by your program. To find out exactly what is going on here however you'll need to post the code, preferably a small example that still segfaults.