确定导致分段错误的代码行? [英] Determine the line of code that causes a segmentation fault?

查看:72
本文介绍了确定导致分段错误的代码行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定错误在代码中导致分段错误的地方

How does one determine where the mistake is in the code that causes a segmentation fault?

我的编译器( gcc )可以显示程序中故障的位置吗?

Can my compiler (gcc) show the location of the fault in the program?

推荐答案

GCC无法做到这一点,但是GDB(a 调试器)。使用 -g 开关编译程序,如下所示:

GCC can't do that but GDB (a debugger) sure can. Compile you program using the -g switch, like this:

gcc program.c -g

然后使用gdb:

$ gdb ./a.out
(gdb) run
<segfault happens here>
(gdb) backtrace
<offending code is shown here>

这里是入门GDB的不错的教程。

Here is a nice tutorial to get you started with GDB.

发生段错误的地方通常只是一个线索代码中的导致错误的地方。给定的位置不一定是问题所在。

Where the segfault occurs is generally only a clue as to where "the mistake which causes" it is in the code. The given location is not necessarily where the problem resides.

这篇关于确定导致分段错误的代码行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆