What is "Debugger"?
I have learned C++ for 2 years and my skills are currently very good (I think, :D).
But during my learning way I never used the "Debugger" to debug the program. Simply, when there was some error, the compiler showed me where (usually at some line) was the problem and then I fixed it.
But what the h**l is the "Debugger"?
Because I have absolutely no idea how it can work.
Could anybody explain me "in few words" this problematic, or if it cannot be explained "in few words", just write here about some book about debugging?
Thanks in advance.
Basically, there are two types of errors.
Compile-time errors are found by the compiler. They are easy to fix.
Run-time errors, however, are more difficult. The compiler won't find them for you. For example, one type of run-time error is a crash. Bad pointer, for example. Memory corruption.
To find these types of errors, we commonly use a debugger, a tool that helps us detect and fix these errors.
My favorite debugger is Visual Studio's debugger. Very powerful and flexible.
I could write an entire guide on how to debug and different techniques and such.
The most common are breakpoints and step-by-step execution. Breakpoints halt execution when a criteria is met (memory location X is written or read; line X is reached; the value of variable X has changed, etc). Step-by-step execution allows you to execute lines one-by-one to "step" through your program and find errors.
The debugger, in essense allows you to view what your variables are, set a breakpoint to stop when a condition is met (such as getting to a particular line is reached).
Single-stepping is a very good way to discover the flow of your code. You can do that by hand, by using the "pretend to be a computer" method, but it's easier if you use a debugger to just press F10 (or whatever) to step to the next line. You can choose to step in to functions to see what happens in the function, or just continue in this function onto the next line "fast-forwarding" the function call (the function is still called, but you will not have to step through the function).
Sometimes, you can also change the value of a variable. Say you have made a mistake that gives you the wrong value on a bool variable. Instead of stopping, repairing the code, compiling again, and then find that you have another mistake 3 lines further down, you can change the bool to the correct value, and then continue the execution with the new value.
One of the most powerful features is that if you have a crash in the code, you can look back at how you got to that point, and most of the time, you can inspec the variables that are in the functions that you passed one the way to the failure and realize that "if i is greater than MAX_VALUES here in func1, then we read outside of the array, and get some rubbish, which causes invalid data to be used in func2, which leads to a divide by zero error in func3." So you can quite easily catch what went wrong, and see how to fix it without recompiling the code with a huge number of print-statements to figure out where it goes wrong.
Edit: If you are usign gcc, there is "Gnu DeBugger", gdb that you can use. If you are using Visual Studio, the debugger is integrated into the Visual Studio toolchain, so you just press some slightly different keys.
Thank you both for nice explanation.