Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

debug code vs release code

Posted on 2004-08-27
5
Medium Priority
?
2,613 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
  • 2
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
bcladd earned 75 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 75 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.
Suggested Courses

783 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