🇯🇵 日本語 | 🇺🇸 English
Advertisement

Lesson 32: Runtime Errors and Segfaults

Causes of segmentation faults in C and how to fix them. A complete guide to runtime errors.

📖 What to learn on this page
✅ Must-know essentials
  • Segmentation fault = accessing forbidden memory
  • NULL deref, out-of-range, use-before-init are the top causes
  • Divide by zero, stack overflow
⭐ Read if you have time
  • The danger of undefined behavior
  • Detection with valgrind / sanitizers
  • Core dumps and post-mortem gdb

What are Runtime Errors — It Compiled, but...

A successful compile means the syntax is correct. A runtime error is when the program crashes when run. The compiler does not tell you about them, so they are harder to debug.
Segmentation Fault
Reading/writing forbidden memory. The most common runtime error.
Bus Error
Memory alignment violations. Environment-dependent.
Floating Point Exception
Division by zero (x / 0).
Infinite loop / freeze
The program never stops. Kill with Ctrl+C.
General approach: (1) use printf to find how far the program gets, (2) print the values of suspicious variables, (3) carefully check array indices and pointers.

Segmentation Fault — Top Causes

Segmentation fault (core dumped) is the error beginners run into most often. It means "you touched memory you were not allowed to".

Top 5 causes

1
Out-of-bounds array access
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;
2
Dereferencing a NULL pointer
int *p = NULL;
*p = 10;  // writing to the address NULL points to -> Segfault

// Fix: NULL-check before use
if (p != NULL) { *p = 10; }
3
Missing & in scanf
int x;
scanf("%d", x);   // NG: uses the "value" of x as an address -> crash
scanf("%d", &x);  // OK: passes the "address" of x
4
Using an uninitialized pointer
int *p;     // uninitialized -> contains garbage address
*p = 42;    // writing to a garbage address -> Segfault

// Fix: always initialize, or allocate with malloc before use
5
Stack overflow (infinite recursion)
int fact(int n) {
  return n * fact(n - 1); // no base case -> recurses forever -> stack overflow
}
// Fix: add if (n <= 1) return 1;

Memory-Related Errors — Hidden Landmines

Memory bugs can still show up as wrong results without a Segfault.
Buffer overrun
Writing past the end of an array corrupts neighboring memory.
char name[5];
strcpy(name, "HelloWorld"); // only 5 bytes but 10+NUL -> overflow

// Fix: strncpy(name, "Hello", sizeof(name)-1);
Memory leak
Forgetting to free memory allocated by malloc leaves unusable memory that keeps piling up.
int *p = malloc(sizeof(int) * 100);
// ... use p ...
// forgot free(p); -> memory leak
Dangling pointer / double free
Using a pointer after it has been freed yields unpredictable behavior.
free(p);
*p = 10;   // writing to freed memory -> undefined behavior
free(p);   // double free -> possible crash

// Fix: set p = NULL; after free

Logic Errors — It Runs But the Answer is Wrong

No crash, but the result is wrong. The hardest kind of bug to debug. printf debugging is your friend.
Integer division pitfall
int a = 7, b = 2;
double avg = a / b;     // -> 3.000000 (not 3.5!)
double avg = (double)a / b; // -> 3.500000 (OK)
Integer / integer truncates the fraction. Cast one side to double.
Operator precedence
if (x & 1 == 0)   // parsed as x & (1==0) - not what you meant!
if ((x & 1) == 0) // OK: explicit parens
Bitwise-vs-comparison precedence is counter-intuitive. When in doubt, parenthesize.
Missing break in switch (fall-through)
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");
}
Stray semicolon after if
if (x > 0);  // ← this ; ends the if statement
{
  printf("positive\n"); // ← always runs!
}

Troubleshooting Checklist

When you hit an error, go through this checklist from top to bottom.

If you see a Segmentation Fault

When results are wrong

When you have lots of compile errors

Advertisement
← Previous lesson
Lesson 31: Compile Errors

Related lessons

Reference
Lesson 31: Compile Errors Dictionary
A catalog of C compile errors and how to fix them.
Loops, Arrays, Strings
Lesson 15: Debugging Techniques
How to debug C programs.
Advanced
Lesson 25: Pointer Basics
Understand C pointers with memory visualization.

FAQ

Q. What is the difference between compile errors and runtime errors?

A. Compile errors are syntax problems detected during compilation; no executable is produced. Runtime errors occur while the program is running — compilation succeeds but the program crashes. Runtime errors are harder to detect.

Q. I got a segmentation fault. What causes it?

A. Segmentation faults are caused by illegal memory access. Main causes: (1) dereferencing a NULL pointer, (2) accessing freed memory, (3) out-of-bounds array access, (4) stack overflow. Review your pointer and memory handling.

Q. How do I debug an infinite loop?

A. (1) Check the loop condition and its exit criterion. (2) Use printf() on loop variables to trace where they diverge from expectations. (3) A step debugger is also effective. Most infinite loops are caused by bad loop conditions or missing increments.

Q. How do I find memory corruption?

A. Memory corruption is hard to find because symptoms appear far from the cause. Effective tools: (1) valgrind (Linux/Mac), (2) sanitizers like UBSan/ASan, (3) careful review of array and pointer usage.

Quiz

Check your understanding!

Q1. What is the main cause of segmentation faults?

Syntax errors
Illegal memory access
Wrong variable names

Causes include NULL pointer dereference, out-of-bounds array access, and use of freed memory. The compiler cannot detect them.

Q2. What happens when you divide by 0?

Compile error
Runtime error (division by zero)
The result is 0

Integer division by 0 is a runtime error. If you divide by a variable, check that it is not zero first.

Q3. How do you fix an infinite loop?

Change the compiler settings
Revisit the loop's exit condition
Change the variable type

Infinite loops happen when the exit condition is never met. Check for missing counter updates and bad condition logic.

Share this article
Share on X Share on Facebook