C++ Question, See if you got what it takes... No Cheating!

Started by
9 comments, last by Dan Smith 24 years, 5 months ago
well i think its crap and wont work since you dont have debug defined
Advertisement
For a start I'd #define the release version to be
#define DebugCode ( code_fragment ) ;
I don't suppose your compiler is replacing all your debug code with #endif's ?
#define??? what the hell is define?
Why did you comment out your #define Debug?
_________________ Best regards, Sherman Chin Director Sherman3D (Malaysia) Sdn Bhd www.Sherman3D.com www.AlphaKimori.com
Here's my quick 2 minute guess.

It's bad, because the two versions behave differently.

When you use the DEBUG version, DebugCode( code_fragment ) becomes { code_fragment }

This variable, pointer, function, whatever it is, still exists.

But on the undefined DEBUG version, all references to DebugCode ( code_fragment ) should be replaced with a value of 1! (It is defined, but has no other value.)
--------------------------------
Also, if you meant to put the space after the DebugCode, then DebugCode will become ( code_fragment ). Not the same either!

I'm not sure if that was intentional, so I answered it both ways.

Pretty sure this is bad for one specific reason: undefined behavior. The preprocessor may choose to either ignore the C++ style comments and define DEBUG, or it may honor the comment and ignore the preprocessor directive.

Also, the non-DEBUG version of the macro should have SOMETHING in its definition. That too is thin ice in terms of compiler/linker implementation.

- Splat

i havent read any other posts, but here's what might be wrong (if it's a typo then...)

#define DebugCode ( code_fragment )

this line is invalid. for macros to accept arguments you have to put the opening parenthesis right after the label WITHOUT A SPACE.

Ok the answer has already been given above but why????

Doing this makes code look very untidy lets see

Debugcode(
{
...
}
)

Now I don't know if it's just me but code within a call be it macro, method, procedure or funtion call is ugly!
The idea is with macros is to repreat simple procedures that are better in-lined with the rest of the code and I generally find that using the key word "inline" in your procedure decleration does exactly the same thing but makes it a lot easyer to debug!

eg.

int inline macrocode()
{
...
}

also no need to put a '/' at the end of each line.

Plus if you use a code editor that hilights key words such as #define
then just put around you code blocks

#ifdef DEBUG
//code segment
#endif

you will be able to easily see the code blocks.

Thouse who use Microsofts Visuel C++ will find that if you compile a debug version the definition is automatically declered so you don't have to add the define or remove it when you are compiling debug or release versions.

Personally I use the good old
#ifdef DEBUG
...
#endif

as Moth already suggested.

What I didn't see above was a definitive answer....what exactly does the compiler do with Dan's statement?

Hello.

First, Dont cheat. Dont compile this to see why it rocks (or why it sucks). Dont post a response if you compiled it to find out. Just look at this code, and tell me whats wrong with it, if anything... Maybe this is the best code ever... Or maybe its the worst.

Look at it, and post your response to why its correct and has no bugs, and why its so great, or why it sucks and wont work the slighest.

Anyway, here's the code:

//#define DEBUG
#if defined (DEBUG)
#define DebugCode( code_fragment ) { code_fragment }
#else
#define DebugCode ( code_fragment )
#endif


I'll be waiting for your answers...

No Cheating!


Dan Smith

D. Smith

This topic is closed to new replies.

Advertisement