1. Home
  2. Computing & Technology
  3. C / C++ / C#

How to Debug Windows Applications Without a Debugger
Fixing Part-Time Bugs in C & C++

By David Bolton, About.com

debugView Utility

One of the harder classes of bugs to find is that which occurs in release code but not debug. I call this a part-time bug. It's often an uninitialized variable to blame. One way to debug this is to build into your application calls to OutputDebugString. In Win 32 this is declared in the Windows.h include file. The somewhat contrived example below shows this. In reality of course the compiler issues warnings and you should never ignore compiler warnings!

OutputDebugString() expects a const char * which is easy enough in C. In C++, though a little finagling is required and that is what the function DbgOutInt() does. It takes a string label and an int variable, uses a stringstream to do an int to string conversion, then uses the string member function c_str(c) to provide access to a C type string and that is what OutputDebugString() requires.

#include "stdafx.h" // Only for Microsoft compilers
#include <iostream>
#include <Windows.h>
#include <sstream>

using namespace std;

void DbgOutInt(string label, int value ) {
stringstream strs;
strs << value;
label.append(strs.str()) ;
const char *c_str =label.c_str() ;
OutputDebugString( c_str ) ;
}

int main(int argc, char* argv[])
{

int a;
int b=a+1;
DbgOutInt("Value of b= ",b) ;
cout << "Value of b = " << b << endl;
return 0;
}

The obvious bug here is that the variable a is never initialized and seeing the debug output

Value of b= -858993459
is a big clue!

Viewing Debug Output

If you run this inside a debugger, you should see the output text in an output window. Both Borland and Microsoft IDEs have this.

From the command line you require a utility to capture the debug output.

The SysInternals.com DebugView Utility

The sysinternals.com website (recently relocated to Microsoft who bought them, but the link still works) provides an excellent utility called DebugView. You'll find it listed on the Miscellaneous Utilities page and it's a quick download.

Just run this and click the help for instructions. You can stop and start logging at any time and save the captured output to disk as a text file.

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. Tools and Utilities
  5. How to Debug Windows C & C++ Applications Without a Debugger>

©2009 About.com, a part of The New York Times Company.

All rights reserved.