GDB Lab "Cheatsheet"

Step 1: compile the file with the -g flag to load the debugging tools:

$ cc -g -o array-bug array-bug.c

Step 2: start the gdb debugger with the name of the executable file you want to debug (not the .c file). This starts gdb but does not start the program itself. Your prompt will change so that you know you are in the debugging environment.

$ gdb array-bug

Step 3: run your program within gdb just to verify the error still occurs

(gdb) run

Step 4: set a breakpoint to stop the program near where you think the problem may be. This can be a line number or a function name

(gdb) break compute_average

Step 5: start the program again, but it will stop at the line number specified or at the function name given

(gdb) run

Step 6: when the program stops, you can use the "step" command to make it advance to the next line in the program and the "print" command to show you the value of the variables at that moment in time.

(gdb) step

(gdb) print i

(gdb) print values[i]

(gdb) print *average

Keep using these until you see where the error occurs. You may need to be patient as you step through the loop

Try something similar to find the error in the function halve() too.