How to find Segmentation Error in C & C++ ? (Using GDB)

What is Segmentation Error?

– It is the runtime mistake caused in view of the memory access infringement. For Eg :- Stackoverflow, read infringement and so forth

We regularly deal with this issue when working out with pointers in c++/c.

In this model we will perceive how to track down the division blunder in the program We will observe which lines cause the division issue blunder.

Note:- I have utilized Linux distro – Ubuntu for this exhibition.

Along these lines, Consider the accompanying bit of C++ Code.

For new developers, investigating mistakes related with pointers can be a bad dream. “Division Fault (center unloaded)” is an unclear blunder message, and it’s much more dreadful when abnormal bugs begin creating the impression that don’t cause division shortcomings – – however that outcome in things like memory getting overwritten in surprising ways. Luckily, apparatuses like Cee Studio, from our support, give new, simpler ways of investigating memory blunders and cushion floods. Cee Studio is an electronic compiler intended to make finding segfaults simple by giving moment and useful input on abuses of memory. However, even without specific instruments, observing issues with pointers is simpler than you might suspect.

This instructional exercise accepts that you have a fundamental information on pointers, for example, can be gained by perusing a pointer instructional exercise. It would assist with being running a framework that has a debugger like GDB, or to at minimum have adequate knowledge of GDB-like debuggers to comprehend the models introduced.

What is a Segmentation issue?

At the point when your program runs, it approaches specific parts of memory. To begin with, you have neighborhood factors in every one of your capacities; these are put away in the stack. Second, you might have some memory, distributed during runtime (utilizing either malloc, in C, or new, in C++), put away on the pile (you may likewise hear it called the “free store”). Your program is simply permitted to contact memory that has a place with it – – the memory recently referenced. Any entrance outside that space will cause a division issue. Division deficiencies are generally alluded to as segfaults.

There are four normal mix-ups that lead to division shortcomings: dereferencing NULL, dereferencing a uninitialized pointer, dereferencing a pointer that has been liberated (or erased, in C++) or that has left extension (on account of clusters proclaimed in capacities), and discounting the finish of an exhibit.

A fifth method of causing a segfault is a recursive capacity that utilizes all of the stack space. On certain frameworks, this will cause a “stack flood” report, and on others, it will just show up as one more kind of division shortcoming.

The technique for troubleshooting these issues is something similar: load the center document into GDB, do a backtrace, move into the extent of your code, and rundown the lines of code that caused the division shortcoming.

Example

#include <stdio.h>
main() {
   int* ptr = NULL;
   *ptr = 1; //trying to access unknown memory location
   printf("%p\n", ptr);
}

Compile the code using ‘gcc –g program_name.c’, and run using ‘./a.out’

Output

[email protected]:~/Cpp_progs$ ./a.out
Segmentation fault (core dumped)

The segmentation error occurred.

Write ‘gdb ./a.out core’

[email protected]:~/Cpp_progs$ gdb ./a.out core
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 <http://gnu.org/licenses/gpl.html>
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:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
/home/soumyadeep/Cpp_progs/core: No such file or directory.
(gdb)

Type ‘r’ and press enter.

Starting program: /home/soumyadeep/Cpp_progs/a.out

Program received signal SIGSEGV, Segmentation fault.
0x000055555555465e in main () at 1230.find_seg_error.c:5
5 *ptr = 1; //trying to access unknown memory location
(gdb)

So we have got the error successfully, now quit the GDB

(gdb) quit
A debugging session is active.

Inferior 1 [process 2794] will be killed.

Quit anyway? (y or n) y
// Segmentation Error Demonstration
// Author - Rohan Prasad
#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.

Also Read: How to Create Array of Objects in Java?

Leave a Reply

Your email address will not be published.