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/
关于段错误的总结
最新推荐文章于 2024-01-08 01:36:57 发布