Linux環境下段錯誤查錯
阿新 • • 發佈:2018-12-12
段錯誤是指訪問的記憶體超出了系統給這個程式所設定的記憶體空間,例如訪問了不存在的記憶體地址、訪問了系統保護的記憶體地址、訪問了只讀的記憶體地址等等情況。
1 、訪問不存在的記憶體地址
#include<stdio.h> #include<stdlib.h> void main() { int *ptr = NULL; *ptr = 0; }
2、 訪問系統保護的記憶體地址
#include<stdio.h> #include<stdlib.h> void main() { int *ptr = (int *)0; *ptr = 100; }
3、 訪問只讀的記憶體地址
#include<stdio.h> #include<stdlib.h> #include<string.h> void main() { char *ptr = "test"; strcpy(ptr, "TEST"); }
4 、棧溢位
#include<stdio.h> #include<stdlib.h> void main() { main(); }
等其他原因。
檢視段錯誤傳送處的簡單方法:
dmesg
dmesg可以在應用程式crash掉時,顯示核心中儲存的相關資訊。通過dmesg命令可以檢視發生段錯誤的程式名稱、引起段錯誤發生的記憶體地址、指令指標地址、堆疊指標地址、錯誤程式碼、錯誤原因等。
--------------------- 本文來自 yuzeze 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/yuzeze/article/details/53144072?utm_source=copy