Linux下C++程式調試及除錯方法

來源:互聯網
上載者:User

引起程式崩潰的原因有很多,主要有以下幾種原因(參考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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.