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.
Example
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; }
Output
Abnormal termination of program