Some C++ Debugging Advice – use #ifdef


If you are an experienced c++ programmer you can stop reading, nothing I cover here will be of any use to you. I’m just writing a little post on a simple little preprocessor trick that c++ programmers have been using for years. Using the #ifdef directive to tag blocks of debugging code.

So lets we have a function that does something like searching through a tree, while debugging we’d like to lets say have a counter to count all the nodes visited during the search but of course in the released version we don’t need this counter using up precious resources. lets have some example code:

unsigned int counter;

void treeSearch::visitNode(node* n)
{
	... some code ...
	counter++;
}

Some beginner programmers would probably do this:

bool debugMode = false;
unsigned int counter;

void treeSearch::visitNode(node* n)
{
	... some code ...
	if ( debugMode) counter++;
}

Yeh, that works but now every time you visit a node, the program evaluates an if statement, not to mention the memory for the counter has still been allocated. Yes in this case memory cost is pretty negligible but that is not always the case. So how can we improve upon this? The answer is by using basic pre-processor directives, more specifically conditional inclusions (#ifdef). Simply put these directives mark code for inclusion into the program during compile time if some condition is met.

so our code would now look as follows:

#ifdef _DEBUG
unsigned int counter;
#endif

void treeSearch::visitNode(node* n)
{
	... some code ...
	#ifdef _DEBUG
	counter++;
	#endif
}

So now those lines will only be included if _DEBUG is defined, what this means is that the counter variable and the code that increments it doesnt get compiled, and your program is as fast as possible and still has debugging functions. You can set the _DEBUG flag in your code manually by using the define directive: #define _DEBUG, or if your IDE supports it, multiple compile profiles, one of which defines _DEBUG before compiling. Visual studio by default defines _DEBUG when compiling using the Debug profile.

So that was a very simple and brief tutorial on something pretty much most C++ programmers know, but i posted it just in case some didnt :)

About these ads

About Bobby
I'm a programmer at Ubisoft. My work interests include Animation and Artificial Intelligence. All opinions are my own!

6 Responses to Some C++ Debugging Advice – use #ifdef

  1. gregory says:

    afaik _DEBUG is specific to windows and visual studio

    i always use #ifndef NDEBUG

  2. ame says:

    Thanks a lot. i’ve been searching unsuccessfully for the exact utility of #ifdef and this is the only place that explained it satisfactorily!

  3. janisozaur says:

    Since my debug code is usually one line outputting values of variables, I prefer having it shorthanded into this form:

    [CODE]
    #define DEBUG

    #ifdef DEBUG
    #define LOG(x) x
    #else
    #define LOG(x)
    #endif

    LOG(cout << "myVar: " << myVar << endl);
    [/CODE]

    I find it much shorter both on keystrokes and lines of code and it is still compatible with what is suggested in the post.

  4. Bobby says:

    for cases like that I find it better to simple add a break point and use the IDE debugger. Its a lot faster and simpler.

    I use the pre-processor debug tags for stuff like logging to file, keeping a record of variables over the course of the program and so on, really slow operations that have absolutely no purpose apart from diagnostics.

  5. saffsaw dnajhah says:

    why would you use double for a counter?

  6. Bobby says:

    no reason at all. it’s just a stupid example… changed it to unsigned int now :P

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: