What is Segmentation Error ?
– It is the runtime error caused because of the memory access violation. For Eg :-Stackoverflow, read violation etc..
We often face this problem when working out with pointers in c++/c.
In this example we will see how to find the segmentation error in the program. We will find which lines causes the segmentation fault error.
Note :- I have used Linux distro – Ubuntu for this demonstration.
So, Consider the following snippet of C++ Code.
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) 188.8.131.5280409-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.
Then just type r and press the enter key .
The output will be something like this showing the erroneous statement.
Starting program: /home/logarithm/Desktop/Test Case/Miccl/a.out
Program received signal SIGSEGV, Segmentation fault.
0x00005555555547de in main () at Sege.cpp:8
Now you have got the line that causes segmentation error.
Exit from debugger and correct the program.
For exiting type quit and press enter.
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- GDB (Step by Step Introduction)
- Analyzing BufferOverflow with GDB
- Segmentation Fault (SIGSEGV) vs Bus Error (SIGBUS)
- OpenCV: Segmentation using Thresholding
- Core Dump (Segmentation fault) in C/C++
- erf | Error functions using cmath in C++
- Error Handling in C programs
- Program error signals
- Else without IF and L-Value Required Error in C
- std::cerr | Error Stream Object in C++ with Examples
- NULL undeclared error in C/C++ and how to resolve it
- How to fix auto keyword error in Dev-C++
- What’s difference between “array” and “&array” for “int array” ?
- C | Loops & Control Structure | Question 1
- C | Macro & Preprocessor | Question 1
- C | Loops & Control Structure | Question 2
- C | Loops & Control Structure | Question 3
- C | Loops & Control Structure | Question 4
- C | Loops & Control Structure | Question 5
- C | Loops & Control Structure | Question 6
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.