关于段错误的总结

1.什么是段错误?
  A segmentation fault (often shortened to segfault) is a particular error
condition that can occur during the operation of computer software. In
short, a segmentation fault occurs when a program attempts to access a
memory location that it is not allowed to access, or attempts to access
a memory location in a way that is not allowed (e.g., attempts to write
to a read-only location, or to overwrite part of the operating system).
Systems based on processors like the Motorola 68000 tend to refer to
these events as Address or Bus errors.
  段错误指访问了内存分配给程序以外的空间。例如访问了不存在的内存地址、访问了系统保护的内存地址、访问了只读的内存地址等等情况。

2.发生段错误的原因?
  2.1访问不存在的内存地址(空指针)
  2.2访问系统保护的内存地址(00)
  2.3访问只读的内存地址
  2.4栈溢出(循环调用)

3.段错误查看方法
  3.1 
dmesg可以在应用程序crash掉时,显示内核中保存的相关信息。通过dmesg命令可以查看发生段错误的程序名称、引起段错误发生的内存地址、指令指针地址、堆栈指针地址、错误代码、错误原因等。
  3.2  -g 使用gcc编译程序的源码时,加上-g参数,这样可以使得生成的二进制文件中加入可以用于gdb调试的有用信息。
  3.3 nm 使用nm命令列出二进制文件中的符号表,包括符号地址、符号类型、符号名等,这样可以帮助定位在哪里发生了段错误。
  3.4 ldd
使用ldd命令查看二进制程序的共享链接库依赖,包括库的名称、起始地址,这样可以确定段错误到底是发生在了自己的程序中还是依赖的共享库中。

4.段错误的调试方法
  4.1 使用printf输出信息
  4.2 使用gcc和gdb
  4.3 使用core文件和gdb
  4.4 使用objdump
  4.5 使用catchsegv
详细的linux环境段错误调试http://blog.163.com/longsu2010@yeah/blog/static/17361234820122761525799/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值