views:

164

answers:

4
+1  Q: 

Debugging Howto

Hi. I am new to C and I am using MS Visual C++ 6.0 for now. I am currently working on sorting algorithms and i want to track the values of each variable automatically. This may provide me insight on how the algorithm does the hard work. That is, I don't want to write what is produced by what on a paper :) Are there are any operators or functions for debugging purposes such as var_dump() in PHP? or How can I improve my debugging abilities? Any other debugging tools for newbies? or any good tutorials on using Visual C++'s built-in debugger? Thank you!..

+3  A: 

In Visual C++, you can set breakpoints on the lines of code that you wrote using the F9 key. You will see a little red dot to the left of that line. Then hit F5 to compile and run.

f10 steps line by line. I think F11 steps into a method.

You can also do trace outputs and debug strings to the output window if you want.

When you set the breakpoints, you can watch variables in a window - and I think the ones on the stack will automatically be in the stack/auto variable window. I am sorry I don't have VC6 in front of me right now to give more details or screenshots.

Tim
A: 

printf/fprintf is the most easy to use debugging tool. It is much easier to analyze what happens if your program logs its activities. I prefer logging to visual debuggers, because it is less interactive and allows to analyze what happened after the program run.

PS. It is better not to pollute stdout, and direct your debug output to file or to stderr.

jetxee
this is a horrible habit. I would use TRACE or debug output to a trace or debug window.
Tim
At the very least wrap the printf calls with #ifdef debug or something. I have no problem with logging to a file, but that is not really interactive/timely.
Tim
Actually I use custom debug print functions in my code, which normally logs to stderr or to file, and can be enabled/disabled with a command line option. For the beginnning a printf is probably enough.
jetxee
Disabling debug output in release version is important Tim, but you miss the point. Complex debugging can't be done using breakpoints and watch windows. The code should be designed to print useful information and jetxee absolutely right.
Ilya
+2  A: 

I think what you're looking for are called "Watches" in Visual Studio. You can add expressions (such as variable names) as items to watch, and as you step through the code their values are automagically updated. You may also be interested in the "Locals" debugging window, which is like Watches except that it is populated with whatever variables are local to the current scope.

Here is a quick tutorial on using Locals and Watches that I found via Google. Also check out this other SO question about best debugging methods.

Parappa
+1  A: 

Use (ALT)(F4) to bring up the variables window This will show the variables for the current statment.

Use (ALT)(F3) to bring up the watch window. You can add variables there, so watch them while they are in scope.

Look under View debug windows, for other options.

EvilTeach