Solved

debug code vs release code

Posted on 2004-08-27
5
2,595 Views
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??
0
Comment
Question by:teken894
[X]
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
5 Comments
 
LVL 11

Accepted Solution

by:
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.

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

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,
-bcl
0
 
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:
#define DEBUGGING

Now you can conditionally compile some code:

#ifdef DEBUGGING
       cout << x
#endif

some other possibilities:

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

Author Comment

by:teken894
ID: 11919289
Thanks,

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

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); ??
0
 
LVL 11

Expert Comment

by:bcladd
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)
  BEGIN
    cout << "Negative" << endl;
  END

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++_ (http://burks.brighton.ac.uk/burks/language/cpp/ticpp/chapt002.htm#Heading140 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,
-bcl
0
 

Author Comment

by:teken894
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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

632 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