Linux下C++程序调试及除错方法

引起程序崩溃的原因有很多,主要有以下几种原因(参考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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值