首页 > 文章列表 > 核心转储(分段错误)在C/C++中

核心转储(分段错误)在C/C++中

C/C++编程 核心转储(coredump) 分段错误(segmentationfault)
210 2023-08-30

在本教程中,我们将讨论一个用于理解C/C++中核心转储(分段错误)的程序。

这种情况发生的原因可能是代码试图在只读内存上写入,或者试图访问损坏的内存位置。

示例

修改字符串文字

int main(){
   char *str;
   str = "GfG";
   *(str+1) = 'n';
   return 0;
}

Accessing out of array index bounds

#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