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

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

PHPz
Release: 2023-09-19 17:21:03
forward
692 people have browsed it

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;
}
Copy after login

Accessing out of array index bounds

#include <iostream>
using namespace std;
int main(){
   int arr[2];
   arr[3] = 10;
   return 0;
}
Copy after login

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;
}
Copy after login

Output

Abnormal termination of program
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template