C gives you full control over the machine, which also means C errors can be subtle — from strict compiler diagnostics to silent undefined behavior that manifests as segmentation faults. This section covers runtime faults and the errno codes set by standard library functions.

Error Codes

ErrorDescriptionFix
malloc() Returns NULL (ENOMEM)Memory allocation failure — malloc cannot allocate requested memoryCheck the return value for NULL, reduce allocation size, and verify no memory leaks with Valgrind
Segmentation Fault (SIGSEGV)Memory access violation — writing to invalid or read-only memoryUse a debugger (gdb), enable AddressSanitizer, and check for null pointer dereferences
Stack OverflowRecursion exceeds stack limit — infinite or too-deep recursive callsAdd a base case to stop recursion, convert to iteration, or increase stack size with -Wl,--stack

Quick Debug

# Compile with all warnings and debug info
gcc -Wall -Wextra -g -o myprogram myprogram.c

# Run under AddressSanitizer
gcc -fsanitize=address -g -o myprogram myprogram.c
./myprogram