Segmentation Fault错误

首先,你的Segmentation Fault错误必须要能重现(废话…)。

   然后,依参照下面的步骤来操作:

   (1)无论你是用Makefile来编译,还是直接在命令行手工输入命令来编译,都应该加上 -g 选项。

   (2)一般来说,在默认情况下,在程序崩溃时,core文件是不生成的(很多Linux发行版在默认时禁止生成核心文件)。所以,你必须修改这个默认选项,在命令行执行:

   ulimit -c unlimited

   表示不限制生成的core文件的大小。

   (3)运行你的程序,不管用什么方法,使之重现Segmentation Fault错误。

   (4)这时,你会发现在你程序同一目录下,生成了一个文件名为 core.*** 的文件,即核心文件。例如,“core.15667”这样的文件。

   (5)用GDB调试它。假设你的可执行程序名为test,则在命令行执行:

   gdb test core.15667

   然后可能会显示出一堆信息:

GNU gdb Fedora (6.8-27.el5)

Copyright (C) 2008 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law. Type “show copying”

and “show warranty” for details.

This GDB was configured as “i386-redhat-linux-gnu”…

warning: Can’t read pathname for load map: Input/output error.

…………………(中间还有很多内容,此处省略)……………………………

Loaded symbols for /usr/lib/libgpg-error.so.0

Core was generated by `./test’.

Program terminated with signal 11, Segmentation fault.

[New process 15668]

0 0x0804c760 in thread _handler () at test.cpp:707

707 CDev* cur_dev = *it_d;

然后我们输入并执行命令 bt :

(gdb) bt

就会得到类似于下面的信息:

0 0x0804c760 in thread _handler () at test.cpp:707

1 0x006b149b in start_thread () from /lib/libpthread.so.0

2 0x0060842e in clone () from /lib/libc.so.6

于是,我们一眼就看出来了:程序是在第707行使用指针时出的问题。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值