Using Visual Studio Debugger

The excellent integrated debugger is perhaps a main reason that many choose Visual Studio. With the debugger you can troubleshoot code that is not running correctly and you can examine freshly written code to make sure it is doing what you expected it to do

Troubleshooting Misbehaving Code

When code does not run correctly, many programmers start by littering their code with a bunch of print statements. I say "litter" because they very seldom actually get deleted - just commented out "in case I need them later." While the print approach can be effective for doing a quick check on the contents of one or two variables, a built in debugger is superior for most problems. In Visual Studio, the most basic debugger commands are:
There is much more you can do with the debugger, but this should get you started. If you forget the hotkeys, click on the Debug menu option.

Homework Practice: Download this project: SpaceFinderBroken. Use the debugger to find the errors listed at the top of SpaceFinder2020Dlg.cpp.

Examining New Code

When I am done writing a tricky piece of code, I often step through it with the debugger to make sure it is doing exactly what I meant it to do. This is more powerful than just running the code to make sure it is correct, because it might appear to work in one test case but under closer scrutiny I can see that it still needs fixing.

Homework Practice: Download this C++ code which does a simple quick sort. Put it inside a Visual Studio project. Use the debugger to step through it to make sure it is functioning as it should. Email comments about your experience to the instructor.


If you want another debugging example, read Using the Debugger. You might need to first read Making a Game Project and Finding Syntax Errors and afterwards read Fixing the bugs.