8.1 - Debugging (2026-10-16)
Overview
Chris Bourke demonstrates GNU GDB as a way to trace C program behavior, inspect memory and variables, and diagnose bugs that a segmentation-fault message alone cannot explain. Using a random-array and average example, he finds and fixes a missing return, an uninitialized sum, an off-by-one loop bound, integer division truncation, and a memory leak, while showing how compiler warnings and Valgrind complement debugging.
Key takeaways
- Compile C programs with GCC’s `-g` flag before using GDB; without debug symbols, source lines, function names, and variable names are unavailable.
- A segmentation fault identifies a failure point, but breakpoints and step-by-step inspection can reveal earlier causes, such as a function returning no array pointer.
- GDB’s `next`, `step`, and `continue` serve different purposes: advance within a function, enter a called function, or resume execution until another breakpoint.
- Watchpoints on loop variables and accumulators can expose boundary errors; an array of length 10 has valid indices 0 through 9, not 10.
- Integer division truncates in C when both operands are integers; casting an operand to `double` preserves a fractional average.
- GDB helps locate logic errors, while Valgrind can independently report memory problems such as the demo’s 40-byte allocation leak, fixed by calling `free`.
Chapters
- A sung interlude built around the repeated refrain “I got to have faith” opens the recording.
- The C debugging lesson begins at approximately 3:19.
- Compile with GCC’s `-g` flag so GDB can show source lines, variable names, and function names instead of only memory addresses.
- Launch GDB with the executable, use `set args 10` for the program’s command-line input, and run the program to reproduce its segmentation fault.
- Use `layout src` and `break 17` to inspect execution at a chosen line; a breakpoint can also target a function such as `random_array`.
- Use `next` to advance within the current function and inspect values such as `n` and array elements with `print`.
- GDB inspection shows that `random_array` fills an array but returns no value, leaving the caller with an invalid pointer; enabling compiler warnings would have flagged the missing return.
- After fixing the return, `step` enters `average`, where inspecting `sum` reveals it was never initialized and must start at zero.
- Watchpoints on `i` and `sum` expose the loop’s extra iteration: for an array of 10 elements, the index must stop before 10.
- At a breakpoint before return, the sum is 45 for the displayed array; `sum / n` still produces 4 because both operands are integers, so one must be cast to `double`.
- After correcting the average calculation, Bourke runs Valgrind to check for invalid memory accesses and other errors.
- Valgrind reports a 40-byte leak allocated by `malloc` at line 28, corresponding to the array returned to `main`.
- Calling `free(arr)` after the array is no longer needed removes the leak and yields a clean Valgrind report.
- Set breakpoints by line, function, or file-and-line, such as `break 14`, `break sum`, or `file.c:17`; remove them with `clear`.
- Use `next` to skip over function calls, `step` to enter them, and `continue` to run until the next breakpoint.
- Inspect variables and expressions with `print`; display an array by dereferencing its pointer and specifying its length, and use `watch` to report changes.
- `backtrace full` displays the call stack and in-scope variables for each frame; GDB can also set variables and call functions interactively.
- The next Thursday lab is a Tic-Tac-Toe debugging exercise with five bugs; students can interact with the program through GDB as it prompts for input.
- The class will begin strings after the break, and there is no lab due the following week.
- Bourke briefly explores the CS50 IDE’s graphical debugger, noting that breakpoints can be clicked into place but launch configuration may be required.
Transcript excerpt
0:40 [music] >> Well, I guess it would be nice if I could touch your body. [music] I know not everybody has got a body like you. >> [music] >> Oh, but I got to think twice before I give my heart [music] away. And I know all the games you play, but guys, I play them, too. [music] Oh, but I need some time off from [music] that emotion. Time to build my fort up up the floor. Oh, when that love >> [music] >> comes down, well, I've been warned, what it takes to storm a man, baby, but I'll show you the door. Cuz I got to have faith. >> [music] >> I got to have
1:26 faith because I got to go for faith I got to have faith the faith the faith the faith. Baby, >> [music] >> I know you're asking me to stay. Say, "Please, please, please, don't go away." You say I'm giving [music] you the blues. Baby, you mean every word you say. I can't help but think of yesterday [music] and another who tied me down to lovable you. Before this river becomes an ocean, goes >> [music and singing] >> before you throw my heart back on the floor. Oh, baby, I >> [music] >> reconsider my foolish [singing] notion. Well, I need someone to hold me, but I wait for something more.
2:13 Yes, I got to have faith. Oh, I got to have faith. >> [music] >> Because I got to have faith, faith, faith, faith. I got to have faith, faith, faith, faith, faith. >> [music] >> Oh, they said to wait, because I got to have faith. I got to have faith. >> [music] >> To have faith. Before this >> [music] >> ever >> [singing] >> comes
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Chris Bourke.