Easiest way to locate a Segmentation Fault

19,754

Solution 1

The easiest way is to use valgrind. It will pinpoint to the location where the invalid access occours (and other problems which didn't cause crash but were still invalid). Of course the real problem could be somewhere else in the code (eg: invalid pointer), so the next step is to check the source, and if still confused, use a debugger.

Solution 2

+1 for Tibors answer.

On larger programs or if you use additional libraries it may also be useful look at the backtrace with gdb: ftp://ftp.gnu.org/pub/old-gnu/Manuals/gdb/html_node/gdb_42.html

Share:
19,754
TZPike05
Author by

TZPike05

Full-time student studying Electrical Engineering. Interned Summer of 2012 at a large Avionics corporation and again from December 2012-August 2013.

Updated on June 04, 2022

Comments

  • TZPike05
    TZPike05 almost 2 years

    I encountered my first Segmentation Fault today (newbie programmer). After reading up on what a segmentation fault is (Thanks for all of the helpful info on this site, as well as Wikipedia's lengthy explanation), I'm trying to determine the easiest way to go about finding where my fault is occuring. It's written in C and the error is occuring on a *NIX based system (I'm not sure which one to be honest... 99% sure it's Linux). I can't exactly post my code as I have numerous files that I'm compiling that are all quite lengthy. I was just hoping for some best practices you have all observed. Thanks for your help.

    P.s. I'm thinking the error is coming from dereferencing a NULL pointer or using an uninitialized pointer. However, I could definitely be wrong.

  • netcoder
    netcoder almost 12 years
    +1 valgrind is very useful, however not much without debug flags.