Kill the Segmentation Fault

Hai friends,
Have you ever compile a c or c++ program using cc or gcc compiler.Then sometimes you might stuck up with segmentation faults.
Segmentation faults occurs when the program refers to an unallocated memory space.
By default the compiler wont show by which instruction the segmentation fault occurs.So you have to go through your entire program to find the culprit.
It is tough to fix a bug even without its line number.
Here is the simple solution to find segmentation faults,

1.Compile the program with “g” flag.
Eg: cc -g filename.c
2.Type gdb a.out
3.It ll take you to the gdb prompt.
4.(gdb)run
5.Give your input values.
6.When there is a segmentation fault the program will stop there.
7.You can see the instruction which causes the segmentation fault.
8.Type “backtrace” without quotes to trace back the segmentation fault.It ll show a number a frames
which are all the reason for segmentation fault.
9.Type “frame frameno” without quotes (to check a particular instruction).
10.There you can find the values of variables by “print variablename” command.

thats all friends,No more worries about segmentation fault.
cheers,Happy programming….
Balachandar.K.M

Advertisements

One thought on “Kill the Segmentation Fault

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s