您好,欢迎来到爱问旅游网。
搜索
您的当前位置:首页Linux下C++程序调试及除错方法

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:

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- awee.cn 版权所有 湘ICP备2023022495号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务