My blog has moved!

You should be automatically redirected . If not, visit
http://www.atriagrawal.com
and update your bookmarks.

About Me

My photo
I am currently pursuing my Masters at University of Southern California in Computer Science.

Sunday, November 20, 2011

Rescue from Segmentation Fault

Segmentation fault.

As soon as I see these golden words glittering on my screen when I execute my program, I can feel the nerve chill going down my spine. Its the last of the errors that I want my program to show. Three months back when I would see this error, I would give up and start from scratch. There was no way I was going to debug it. But in these three months I did quite a lot of debugging and thanks to Professor Cheng's assignments, because of whom I have come out of the "FEAR" of Segmentation Fault. Now when I get any such error, I say, okay lets see.... gotcha, here is the problem. Earlier the reaction was something like "forget it!!"

Like my Proff. said, "I don't have a magic wand to remove Segmentation faults". I didn't find any foolproof method to get rid of the segmentation faults, but one of the following methods always works for me.

You should be firstly very clear with why segmentation faults occurs. It can occur due to memory leaks, insufficient memory and other memory related issues. This does not happen very often.The most common mistake because of which this error occurs is that we call method on a NULL value or we try to access some memory location which we haven't allocated, or we are deleting some object which doesn't exist.

Example:

SomeClass *temp=NULL;
temp->someFunction();

/*Even if you have initialized temp to a new object, if somewhere in your code the value of temp is getting set to NULL, it will give segmentation fault.*/

or

SomeClass *temp[10];
cout<<temp[15]->someFunction();

Some debugging techniques:

1) Our old friend GDB(GNU Debugger). This is one of the best tools to debug your code. To use GNU debugger make sure you compile your code by using the '-g' switch. To find where your code is causing a segmentation fault do the following. Start gdb by typing:

gdb {executablefile}
run {command line options(if any)}

Run the above commands on terminal without the braces, those are just a way of representation.

After doing this you will get segmentation fault. Now type "where" or "bt". This will take you to the exact line number where your code is causing the error. BT is used to back trace from where the error occurred and it will show the full stack.

2) Valgrind: This is a piece of software which is very helpful in debugging segmentation fault errors. As of now this is only available for Linux systems. To use Valgrind, after installing valgrind, just type:

./valgrind {executablefile} {command line options} ----->(without the braces)

The output of this would be a full stack trace and the exact line number where the problem is occurring.

3) Try writing printf/cout statements at several points and find where your code is breaking. This is not a very good method as it is very tedious and sometimes when Segmentation faults occur you would not get the output of any printf's. But it does work sometimes so it can be given a shot.

4) Increase the size of array: One trick to solve this can be increasing the size of the arrays(wherever we are using an array) that we are using in our code. In case there is a problem wherein we are trying to access an index number which is not allocated, then the problem will be resolved.


This error is similar to NullPointerException that we get in JAVA. Just that in java we can catch the exception and see the full stack trace, thus its easier to debug this problem in java.

No comments:

Post a Comment