引起程序崩溃的原因有很多,主要有以下几种原因(参考eventhelix.com)
1.Invalid Array Indexing 无效的数组索引
Data1 a; // Corrupted when b is indexed with 0xFFFFFFFF (-1) 由于错误的数组索引,如b[-1]可能破坏a
int b[100]; // Declaration of b. Keep in mind that array indexing is a signed operation
Data2 c; // Corrupted when index into b is greater than 99 由于错误的数组索引,如b[100]可能破坏c
2.Un-initialized Pointer Operations 未初始化的指针操作
3.Unauthorized Buffer Operations 未经许可的缓冲操作
4.Illegal Stack Operations 非法的堆栈操作
5.Invalid Processor Operations 无效的处理器操作
--Divide by zero attempted by application
--Program running in user mode attempted to execute an instruction that can only be executed in supervisor (kernel) mode.
--Program attempted access to an illegal address. The address might be out of range or the program might not have the privilege to perform the access. For example, a program attempting to write to read only segment will result in an exception.
--Misaligned access to memory also results in an exception. Most modern processors restrict long word reads to addresses divisible by 4. An exception will be raised if a long word operation is attempted at an address that is not divisible by 4. (See the byte alignment and ordering article for details)
6.Infinite Loop 无限循环
常用的检测和排错方法
1.断言Assert
2.输出中间结果print/Log
3.使用调试器GDB/DDD
4.使用检测工具memwatch,Valgrind,Parasoft C++ Test/Insure C++
FAQ:
1.如何使程序在出错时产生core dump文件
vi /etc/profile
# No core files by default
# ulimit -S -c 0 > /dev/null 2>&1
ulimit -S -c unlimited > /dev/null 2>&1
2.如果从core文件中得出所需的信息
gdb program core.xxx
bt
info locals
frame x
Links:
http://www.eventhelix.com/RealtimeMantra/Basics/debugging_software_crashes.htm
http://www.eventhelix.com/RealtimeMantra/Basics/debugging_software_crashes_2.htm