Core Dump (Segmentation fault) in C/C++

In this tutorial, we will be discussing a program to understand core dump (segmentation fault) in C/C++.

It happens due to reasons like when code tries to write on read only memory or tries to 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>
int main(void){
   int* p = malloc(8);
   *p = 100;
   *p = 110;
   return 0;


Abnormal termination of program