Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

Core Dump (Segmentation Fault) ใน C/C++


ในบทช่วยสอนนี้ เราจะอภิปรายโปรแกรมเพื่อเข้าใจ core dump (segmentation fault) ใน C/C++

มันเกิดจากเหตุผลเช่น โค้ดพยายามเขียนบน read-only memory หรือพยายาม access corrupt memory location

ตัวอย่าง

Modifying a string literal

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;
}

Accessing an address which is freed

#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