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