Skip to content
AgileLogics
4 min read

Debugging Basics: Warnings and Segfaults

Prerequisites: Comments & code style

Always compile with warnings turned on — C's compiler will happily compile code that's almost certainly a bug unless you ask it to tell you:

gcc -Wall -Wextra hello.c -o hello

A segmentation fault ("segfault") means the program tried to access memory it has no right to — dereferencing an invalid pointer, or writing past the end of an array, are the two most common causes. Unlike an exception in Java or Python, there's no message explaining why — just an abrupt crash — which is exactly what a debugger like gdb is for: it stops execution at the crash and shows exactly which line and which variables caused it.

Common mistakes

  • Ignoring compiler warnings — -Wall -Wextra catches a large fraction of the mistakes covered in this course (uninitialized variables, mismatched printf format specifiers, and more) before you ever run the program.