Home  >  Article  >  Backend Development  >  Core dump (segmentation fault) in C/C++

Core dump (segmentation fault) in C/C++

PHPz
PHPzforward
2023-09-19 17:21:03618browse

Core dump (segmentation fault) in C/C++

In this tutorial, we will discuss a program for understanding core dumps (segmentation faults) in C/C.

This may happen because code is trying to write on read-only memory, or trying to access a corrupted memory location.

Example

Modify 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 the released address

#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

The above is the detailed content of Core dump (segmentation fault) in C/C++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete