Causes of segmentation faults in C and how to fix them — your complete guide to runtime errors.
x / 0).Segmentation fault (core dumped) is the error beginners run into most often. It means "you touched memory you weren't allowed to."int a[5]; a[10] = 99; // only a[0]..a[4] exist; writing a[10] = instant crash // Common mistake: wrong loop bound for (int i = 0; i <= 5; i++) // accesses a[5] when i=5 -> NG a[i] = i;
int *p = NULL; *p = 10; // writing to the address NULL points to -> Segfault // Fix: NULL-check before use if (p != NULL) { *p = 10; }
int x; scanf("%d", x); // NG: uses the "value" of x as an address -> crash scanf("%d", &x); // OK: passes the "address" of x
int *p; // uninitialized -> contains garbage address *p = 42; // writing to a garbage address -> Segfault // Fix: always initialize, or allocate with malloc before use
int fact(int n) { return n * fact(n - 1); // no base case -> recurses forever -> stack overflow } // Fix: add if (n <= 1) return 1;
char name[5]; strcpy(name, "HelloWorld"); // only 5 bytes but 10+NUL -> overflow // Fix: strncpy(name, "Hello", sizeof(name)-1);
free memory allocated by malloc leaves unreachable memory that accumulates over time.int *p = malloc(sizeof(int) * 100); // ... use p ... // forgot free(p); -> memory leak
free(p); *p = 10; // writing to freed memory -> undefined behavior free(p); // double free -> possible crash // Fix: set p = NULL; after free
int a = 7, b = 2; double avg = a / b; // -> 3.000000 (not 3.5!) double avg = (double)a / b; // -> 3.500000 (OK)
if (x & 1 == 0) // parsed as x & (1==0) - not what you meant! if ((x & 1) == 0) // OK: explicit parens
switch (n) { case 1: printf("one\n"); // no break -> case 2 and case 3 also run case 2: printf("two\n"); case 3: printf("three\n"); }
if (x > 0); // ← this ; ends the if statement { printf("positive\n"); // ← always runs! }
A. Compile errors are syntax problems caught during compilation; no executable is produced. Runtime errors occur while the program is running — the code compiles but the program crashes. Runtime errors are harder to detect.
A. Segmentation faults come from illegal memory access. The main culprits: (1) dereferencing a NULL pointer, (2) accessing freed memory, (3) out-of-bounds array access, and (4) stack overflow. Review your pointer and memory handling.
A. (1) Check the loop condition and exit criterion. (2) Use printf() on loop variables to see where they diverge from your expectations. (3) A step debugger is also effective. Most infinite loops come from bad loop conditions or a missing increment.
A. Memory corruption is tricky because the symptoms often appear far from the actual cause. Effective tools include (1) valgrind (Linux/Mac), (2) sanitizers like UBSan and ASan, and (3) careful review of your array and pointer usage.
Check your understanding.
Common causes include NULL pointer dereferences, out-of-bounds array access, and using freed memory. The compiler can't catch any of these.
Integer division by 0 is a runtime error. If you're dividing by a variable, check that it's not zero first.
Infinite loops happen when the exit condition is never met. Check for a missing counter update or flawed condition logic.