๐ WAYPOINT LESSON
Runtime Failures
โญ beginnerโณ 14 min read๐ Lesson 69 of 225
Segfaults, wrong outputs, and hangs: the three failure families and their usual suspects.
Family 1: the crash (segfault)
The process dies with Segmentation fault. Usual suspects, in order:
- NULL or uninitialized pointer dereference
- out-of-bounds array index
- use-after-free / dangling pointer
- writing through a bad pointer in a string function
A segfault tells you WHERE it died (the stack trace) but not WHY โ the bad address was usually produced much earlier.
Family 2: wrong output, no crash
The sneakiest. Causes: off-by-one loop bounds, wrong comparison operator, accumulating into the wrong variable, format-specifier mismatch, integer division where you meant floating division. The defense is tests with known answers โ exactly what this course's challenges run against your code.
Family 3: the hang
An infinite loop: the counter never moves toward the bound, or the termination condition can never become false. Check that something in the loop body changes every iteration.
The debugging loop
- reproduce โ find the smallest input that fails
- predict โ say out loud what SHOULD happen
- instrument โ print the values at each step (printf is the oldest debugger and still a great one)
- narrow โ binary-search the pipeline: is the input already wrong, or the processing?
- fix the cause, not the symptom โ a swallowed crash returns as a worse bug