You are here:About>Computing & Technology>C / C++ / C#> Tools and Utilities> How to Debug Windows C & C++ Applications Without a Debugger
About.comC / C++ / C#
debugView Utility
Newsletters & RSSEmail to a friendSubmit to Digg
Elsewhere on the Web

Sysinternals Website

How to Debug Windows Applications Without a Debugger

From David Bolton,
Your Guide to C / C++ / C#.
FREE Newsletter. Sign Up Now!

Fixing Part-Time Bugs in C & C++

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.

Elsewhere on the Web

Sysinternals Website

 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.