Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


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   -  Reputation: 122

Posted 24 November 1999 - 03:36 PM

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


Sponsor:

#2 jobu   Members   -  Reputation: 122

Posted 15 November 1999 - 05:49 AM

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

#3 MikeD   Members   -  Reputation: 158

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+   -  Reputation: 288

Posted 15 November 1999 - 06:46 AM

#define??? what the hell is define?

#5 sherman   Members   -  Reputation: 151

Posted 15 November 1999 - 08:12 AM

Why did you comment out your #define Debug?

#6 Gorky   Members   -  Reputation: 122

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   -  Reputation: 122

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   -  Reputation: 130

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   -  Reputation:

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

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.


#10 MikeD   Members   -  Reputation: 158

Posted 17 November 1999 - 02:54 AM

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?


#11 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

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
macro.

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.



PARTNERS