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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now