In development phase it is a good practice to pass to the complier the following flags:
CXXFLAGS=${CXXFLAGS} -fsanitize=address -fsanitize=leak -fsanitize=undefined
The compiler will add fences in the executable to detect at run time wrong access to memory, memory leakage and undefined behaviour as in zero division, overflow, and unsafe casting.
Advanced IDE like Visual C++ or Eclipse have facilities to debug your
programs by inspecting each line of it during execution.
However there are simpler solutions.
In Linux you can use the program gdb
and in MacOsX lldb
.
Compile your files with the g++
compiler using the flag -g
.
Then lunch your program under gdb
.
gdb calc
Set a break point at a line or function. For example
(gdb) break 52
Lunch the execution:
(gdb) run calc
And continue with these commands:
s
to step each line following inside functionsn
to execute the next line without going inside functionsprint variable
to print a variablelldb
works similarly. See for example this link