C/C++ 中的核心轉儲(段錯誤)
本教程將討論一個瞭解 C/C++ 中核心轉儲(段錯誤)的程式。
它發生的原因包括程式碼嘗試寫入只讀記憶體或嘗試訪問損壞的記憶體位置。
示例
修改字串文字
int main(){ char *str; str = "GfG"; *(str+1) = 'n'; return 0; }
訪問超出陣列索引範圍
#include <iostream> using namespace std; int main(){ int arr[2]; arr[3] = 10; return 0; }
訪問已釋放的地址
#include <stdio.h> #include<alloc.h> int main(void){ int* p = malloc(8); *p = 100; free(p); *p = 110; return 0; }
輸出
Abnormal termination of program
廣告