debug code vs release code

Posted on 2004-08-27
Last Modified: 2012-06-27
How can you make c++ execute code that is for debugging only, and not for release builds.

such as

int x = 3;

x +=x

//debug only:
cout << x

ive heard of debug-code, but how do you use it??
Question by:teken894
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 11

Accepted Solution

bcladd earned 25 total points
ID: 11918818
There are a lot of different ways to do it but one is to use a DEBUG macro.

#define DEBUG(x) x
#define DEBUG(x)

Then if you compile with USEDEBUG set (which you do in your debug builds), DEBUB(somcode) will emit the some code and if you don't set USEDEBUG then nothing is emitted.

DEBUG(cout << x);

This is a very, very simple use of a macro with many potential problems but it is a place to start putting your head around conditional compilation and having code that compiles in in one case and out in another.

Good luck,
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 25 total points
ID: 11918934
It will be easier to understand this.

When generating a DEBUG version, just include this into your code:

Now you can conditionally compile some code:

       cout << x

some other possibilities:

#ifndef DEBUGGING              // if not in debugging mode
       // do something
       // do other thing

Author Comment

ID: 11919289

#define DEBUG(x) x
#define DEBUG(x)

what is the DEBUG(x) x and DEBUG(x) for?

all i do is DEBUG(//someting); to only execute in debug?

can i do RELEASE(//something); ??
LVL 11

Expert Comment

ID: 11920573
#define DEBUG(x) x

#define begins the definition of a _macro_. A macro is a piece of text that the C preprocessor will replace when it sees it. Back in the Pascal days, some people tried to make C look more like Pascal with:

#define BEGIN {
#define END }

Whenever the preprocessor sees the character sequence BEGIN (outside of a comment or a string), it is replaced with the remainder of the line (just "{" in this case) and the same with END, so

if (x < 0)
    cout << "Negative" << endl;

is preprocessed into

if (x < 0)
    cout << "Negative" << endl;

Macros can take "parameters". Parameters are not typed, they are simply strings of text. The macros appear in parentheses after the name of the macro in the definition, separated by commas. They also appear, separated by commas, when the macro is used. The following example is slightly modified from Eckel's _Thinking in C++_ ( and just above for discussion of PRINT; you can read more in the full book, available on-line all over the place).

// A macro to display a string and a value.
#define PRINT(STR, VAR)  cout << STR " = " << VAR << endl

Two parameters, so the preprocessor only replaces if there are two parameters in the "call".

PRINT("X", x);

is processed into

cout << "X" " = " << x << endl;

The text is EXACTLY the body of the macro definition (the rest of the line) with STR replaced with the text before the comma and VAR replaced with the text after the comma (actually after the whitespace after the comma). Note that "X" " = " is evaluated by the compiler as "X = " so this has the intended effect.

Back to

#define DEBUG(x) x

This just replaces every "call" to DEBUG(<some code here>) with <some code here>

#define DEBUG(x) x

DEBUG(cout << "One two three" << endl);

preprocesses into

cout << "One two three" << endl;

But if we change the definition of DEBUG(x):

#define DEBUG(x)

DEBUG(cout << "One two three" << endl);

Now DEBUG(x) is defined to be empty (nothing else is on the line beyond the name and parameter list) so it preprocesses into


The conditional compilation is limited to the definition of the macro as an identity mapping (for debug) or a null mapping (for release).

You could define RELEASE macros similarly but I can't think that you would want to. You would want to have all the code in the debug build for debugging puroses. But if you wanted, you could make any macro you like.

Take a look at Eckel's book (especially the end of chapter 3): he talks about the power and risk of using macros.

The other conditional compilation method works well; I find it harder to read the code but if you only use if debug blocks, the idiom could be learned and the code read pretty well.

Again, good luck,

Author Comment

ID: 11923332
Thanks for the reply, though lenghty indeed, hope i didn't waste any time asking silly questions,

anyway, I understand now, just a beginner my self...Hoping to learn C++, so my code is pretty buggy...this helps a LOT!!
thx again

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question