Solved

debug code vs release code

Posted on 2004-08-27
5
2,579 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 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 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

17 Experts available now in Live!

Get 1:1 Help Now