Mostly we use printf statements in c and cout in c++ to debug to program , to check at which line error is coming.
This is basically a time consuming method, good for codes of short length but when code is long , not applicable, as you need to put printf statements after each line and compile it again and again. At this point the use of a debugger tool comes.
gdb is most popular debugger when it comes to c and c++.
Its a program, which runs other programs, allowing the user to exercise control over these programs, and to examine variables when problems arise.
It helps in getting information like core dump( at which line it has happened),an error occurs while executing a function, what line of the program contains the call to that function, and what are the parameters.
If we want to see value in a variable during the time of execution of program that we can see too.
Even though GDB can help you in finding out memory leakage related bugs, but it is not a tool to detect memory leakages.
GDB cannot be used for programs that compile with errors and it does not help in fixing those errors.
Normally used commands in gdb are : b main(put break at starting of program), b anyline(puts breakpoint at a particular line),r (run program),r argv(run program with arguments),p ( printing a variable), n (next line), q(quit).
How to use gdb
First compile the file with option -g , like gcc -g test.c. It will create a a.out file containing the debugging information that lets you use variables and function names inside GDB, rather than raw memory locations.
Run gdb a.out. This will not run the actual program. It will open gdb.
Now according to which code section in the program you want to check , put breakpoints in the code . Or you debug whole program from starting.
Using p and r you can see the execution of program line by line.