Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

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

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Dan Smith   Members   


Posted 24 November 1999 - 03:36 PM


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 }
#define DebugCode ( code_fragment )

I'll be waiting for your answers...

No Cheating!

Dan Smith

#2 jobu   Members   


Posted 15 November 1999 - 05:49 AM

well i think its crap and wont work since you dont have debug defined

#3 MikeD   Members   


Posted 15 November 1999 - 06:00 AM

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 ?

#4 firahs   GDNet+   


Posted 15 November 1999 - 06:46 AM

#define??? what the hell is define?

#5 sherman   Members   


Posted 15 November 1999 - 08:12 AM

Why did you comment out your #define Debug?

#6 Gorky   Members   


Posted 15 November 1999 - 08:33 AM

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.

#7 Splat   Members   


Posted 15 November 1999 - 10:31 AM

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

#8 foofightr   Members   


Posted 17 November 1999 - 12:09 AM

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.

#9 Anonymous Poster_Anonymous Poster_*   Guests   

Posted 17 November 1999 - 02:28 AM

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

Doing this makes code look very untidy lets see


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!


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

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.

#10 MikeD   Members   


Posted 17 November 1999 - 02:54 AM

Personally I use the good old
#ifdef DEBUG

as Moth already suggested.

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

#11 Anonymous Poster_Anonymous Poster_*   Guests   

Posted 24 November 1999 - 03:36 PM

Ok this is what it looks like to me...

You have a macro called DebugCode that
does something unspecified. It only does
this when DEBUG has been defined. When
DEBUG is commented out (for your release
version of course), it defines a blank

This is cool because when DEBUG is defined
you can have all awesome messages for the programmer and log files and stuff, then you
just change 1 line to release it.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.