This tutorial explains, How to find method which generate segmentation fault error in large program. Basically, segmentation fault is a common condition that causes programs to crash; they are often associated with a file named core .Segmentation faults are caused by a program when it trying to read or write an illegal memory location or trying to access memory location who’s permissions are not granted.
Main Reasons of segmentation fault error is :
- Accessing elements which are out bounds of an array – if array is declared with fixed size and program trying to refer the element with index less than lower bound or greater than upper bound of array. so make sure that iterators are within range of lower and upper bound of array.
- Accessing uninitialized pointer – if program trying to access values using pointer which are pointing to invalid locations. means either pointer variable is not initialized or not pointing to valid address location before accessing the values. so make sure that you have initialized all pointers to a valid area of memory.
- Incorrect use of the “address of” operator and “dereferencing” operator
“address of” operator (&) returns the memory address of any variable. if you forget to use “&” with each variable in a scanf functions call. The Scanf function requires the address of the variables to read values from console. so make sure that you used & and * operator correctly in the program.
It always difficult to find exact location of segmentation fault error in large program. so debugging of program correctly can give method details which generates this error.
Steps to debug C or CPP program in Ubuntu
To Install gdb on ubuntu –
<span style="font-family: book antiqua,palatino,serif;">sudo apt-get install libc6-dbg gdb valgrind</span>
To Compile C / CPP Program
<span style="font-family: book antiqua,palatino,serif;">gcc -g testfile.cc -o testfile
To Run debugger
<span style="font-family: book antiqua,palatino,serif;">gdb ./testfile</span>
To Run program
<span style="font-family: book antiqua,palatino,serif;">run</span>
To Print Stack trace
<span style="font-family: book antiqua,palatino,serif;">bt</span>
The stack trace shows the name of method where segmentation fault error has been occurred. so you can check that method for above reasons of error cause.
What is stack smashing error ?
As we know, memory structure used in many programming languages to store state that is variable values, for instance is known as the “stack.” The Program control flow is also managed by the stack. Some specific space is allocated to stack . if data exceeds beyond the space allocated, then that additional data can overwrite other data stored on the stack and cause problems for other variables and program control flow. that is the cause of stack smashing error. Debugging can be used to track such a variables which overflow the stack space limit.
This video tutorials shows , how to run debugger to track segmentation fault error and stack smashing error. Here is the TestSegFault.cpp_1.zip(46 downloads) of program file in the tutorial.