top of page

Segmentation fault in C++.


Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.

Example of Segmentation fault.

Accessing an address that is freed :

// C program to illustrate
// Core Dump/Segmentation fault
#include <stdio.h>
#include<alloc.h>
int main(void)
{
 // allocating memory to p
 int* p = malloc(8);
 *p = 100;
  
 // deallocated the space allocated to p
 free(p);
  
 // core dump/segmentation fault
 //  as now this statement is illegal
 *p = 110;
  
 return 0;
}

Accessing out of array index bounds :

// C++ program to demonstrate segmentation
// fault when array out of bound is accessed.
#include <iostream>
using namespace std;
 
int main() 
{
 int arr[2];
 arr[3] = 10;  // Accessing out of bound
 return 0;
}

Dereferencing uninitialized pointer:

A pointer must point to valid memory before accessing it.

// C program to demonstrate segmentation
// fault when uninitialized pointer is accessed.
#include <stdio.h>
 
int main() 
{
 int *p;
 printf("%d",*p);
 return 0;
}

How to find that error using gdb?

// Segmentation Error Demonstration 
// Program1.cpp
#include <iostream> 
using namespace std; 
  
int main() 
{ 
 int* p = NULL; 
  
 // This lines cause the segmentation  
 // error because of accessing the  
 // unknown memory location. 
 *p = 1; 
  
 cout << *p; 
 return 0; 
} 

How to find that error using gdb? Let’s say your file name is saved as Program1.cpp. Head our to your terminal (Be in the directory in which this Program1.cpp is available)

Step 1: Compile it. $ gcc -g Program1.cpp (in my case). Step 2: Run it. $ ./a.out (it is Object File) If it shows Segmentation fault (core dumped) then follow following steps. Step 3:Debug it $ gdb ./a.out core Your output will look like something this:

GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: . Find the GDB manual and other documentation resources online at: . For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from ./a.out...done. /home/logarithm/Desktop/Test Case/Miccl/core: No such file or directory. (gdb)

———————————————————————————— Then just type r and press the enter key . The output will be something like this showing the erroneous statement. ———————————————————————————–

(gdb) r Starting program: /home/logarithm/Desktop/Test Case/Miccl/a.out

Program received signal SIGSEGV, Segmentation fault. 0x00005555555547de in main () at Sege.cpp:8 8 *p=1; (gdb) ———————————————————————————— Now you have got the line that causes segmentation error. Exit from debugger and correct the program. For exiting type quit and press enter. ———————————————————————————– (gdb) quit A debugging session is active.

Inferior 1 [process 3617] will be killed.

Quit anyway? (y or n) y

———————————————————————————– So, wow you have resolved the head torturing segmentation fault.

留言


bottom of page