Compiling a Program

The straightforward way to compile a program on a Linux system is:
g++ gdb-example.cpp -o example
However, if the program might need debugging, it's better to add the following options to the compiler directive:
g++ example.cpp -g -Wall -Werror -o example
The '-Wall' option enables all warning flags, and '-Werror' marks the warnings as errors. Even if there are no warnings or errors during the compilation, the debug symbols would still be present.
The GDB utility can be used simply with the 'gdb' command. This loads the compiled program and presents the '(gdb)' prompt.
To run the program in the debugger:
(gdb) run
To set breakpoints, use 'break' to set the breakpoint line numbers before running the program:
(gdb) break 5
(gdb) run

The output should display the contents of the buffers at the point where execution was stopped. Just in case wwe don't know where the program had stopped, the 'list' command would print the 10 lines of code around the breakpoint.

Reading and Manipulating Variables

(gdb) print variable
Change the variable with:
(gdb) set variable = value
We can also watch for changes in a variable's buffer with:
(gdb) watch variable

References