Monday, 24 June 2013

Segmentation faults, valgrind and pointers

If you have segmentation faults in C the fastest way to fix them is by using valgrind. First compile the program with the -g parameters e.g. gcc -g main.c -o main. The -g adds debug data so you know exactly in which line you've the segmentation fault as soon as the valgrind runs. Then run the program with valgrind. valgrind ./main parameter1 parameter2. That's it, now valgrind will show you exactly where the segmentation fault occurred and it will be easy to identify why.


No comments:

Post a Comment