Jump to content


Preprocessor is evil


22 replies to this topic

#1 dave_

    Senior Member

  • Members
  • PipPipPipPip
  • 584 posts

Posted 03 October 2005 - 09:34 AM

One of my colleagues has just been got by a nasty little bug. We tracked it down to this sort of thing


#define SOME_DEFINE 1


... Some code


#if SOME_DEFINE == 0


... some more code


else


... something else


#endif


Whats the problem? Its the missing '#' infront of the else. It silently ignores all the code after the #if

#2 .oisyn

    DevMaster Staff

  • Moderators
  • 1810 posts

Posted 03 October 2005 - 11:57 AM

Preprocessors are good, they are excellent for build configuration settings and for multiplatform builds. We port games to/from pc, ps2 and xbox, and preprocessor directives are a great tool for supplying platform specific code.

Preprocessors aren't evil just because of the example you provided. There are tons of spelling errors in code that compile perfectly but don't do what's expected.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#3 bladder

    DevMaster Staff

  • Moderators
  • 1057 posts

Posted 03 October 2005 - 11:59 AM

anything without the '#' is not preprocessor code. And you've defined SOME_DEFINE as 1. So #if SOME_DEFINE == 0 is false and everything after that is ignored after the compiler, doesnt even try to compile it. So it will not catch the "else" mess up within the #if and #endif block. Try changing the SOME_DEFINE to 1 and it chould complain then.

#4 leblebi

    New Member

  • Members
  • Pip
  • 4 posts

Posted 03 October 2005 - 03:33 PM

By the same logic every command can be made evil:

for ( i=0;i<50;i++ );

  printf ( "Now counting: %d\n", i );



#5 NomadRock

    Senior Member

  • Members
  • PipPipPipPip
  • 785 posts

Posted 03 October 2005 - 04:01 PM

if you just changed that 50 to a 1 then it would be correct :)
Jesse Coyle

#6 bramz

    Valued Member

  • Members
  • PipPipPip
  • 189 posts

Posted 03 October 2005 - 07:54 PM

He is right though. The preprocessor _is_ evil (or better known as: macros are evil). That doesn't mean we don't want to use it though. It's a very powerfull tool, and we wouldn't want to go on without. But you must be carefull when playing with black magic :)

Bramz
hi, i'm a signature viruz, plz set me as your signature and help me spread :)
Bramz' warehouse | LiAR isn't a raytracer

#7 baldurk

    Senior Member

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 03 October 2005 - 08:07 PM

leblebi said:

By the same logic every command can be made evil:
for ( i=0;i<50;i++ );
  printf ( "Now counting: %d\n", i );

NomadRock said:

if you just changed that 50 to a 1 then it would be correct :)

unless your code was very bad and you had another i variable outside the loop, it'd complain because i goes out of scope with the for loop.
baldurk
He who knows not and knows that he knows not is ignorant. Teach him.
He who knows not and knows not that he knows not is a fool. Shun him.

#8 .oisyn

    DevMaster Staff

  • Moderators
  • 1810 posts

Posted 03 October 2005 - 11:09 PM

bramz said:

He is right though. The preprocessor _is_ evil (or better known as: macros are evil). That doesn't mean we don't want to use it though. It's a very powerfull tool, and we wouldn't want to go on without. But you must be carefull when playing with black magic :)

Bramz

Well yes, you shouldn't use macro's for things that should actually be constants or inline functions. But to say that the whole construct of macro's are evil... Otherwise exceptions would be evil too, as you can use them for normal program flow [and more of such examples] :)

baldurk said:

unless your code was very bad and you had another i variable outside the loop, it'd complain because i goes out of scope with the for loop.

Look again, he doesn't define i inside the for statement :)
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#9 NomadRock

    Senior Member

  • Members
  • PipPipPipPip
  • 785 posts

Posted 04 October 2005 - 12:30 AM

baldurk said:

unless your code was very bad and you had another i variable outside the loop, it'd complain because i goes out of scope with the for loop.

you will notice that i was not declared in the loop, and you will also notice that he uses printf and so is probably using C which doesnt even allow you to declare variables inside the loop. The variable i is most likely declared at the beginning of the enclosing function
Jesse Coyle

#10 moe

    Valued Member

  • Members
  • PipPipPip
  • 270 posts

Posted 04 October 2005 - 01:08 AM

Maybe I am wrong but I think the “i” is not the point. It’s the semicolon after the for statement. It would be more readable like this:


for( i=0;i<50;i++)

	;

	printf( “Now counting: %d\n”, i );

witch is the same as:

for( i=0;i<50;i++)

{

	;

}

printf( “Now counting: %d\n”, i );


or am I now missing something :)

#11 NomadRock

    Senior Member

  • Members
  • PipPipPipPip
  • 785 posts

Posted 04 October 2005 - 01:18 AM

you are right moe, I was making a joke. but it *is* true that the output would be the same with or without the semicolon if the loop only went 0 to less than 1 anyway :)
Jesse Coyle

#12 dave_

    Senior Member

  • Members
  • PipPipPipPip
  • 584 posts

Posted 04 October 2005 - 09:28 AM

Cross platform and debug information (such as leak tracking and assertions) are the only things I use the preprocessor for. (I had to remove the code cos of NDAs)

I prefer to see the C++ code, incase you have to debug it. Its easier than having to export the generated code.

And while I'm at it, printf's aren't too good. They arent type-safe.

#13 dave_

    Senior Member

  • Members
  • PipPipPipPip
  • 584 posts

Posted 04 October 2005 - 09:30 AM

leblebi said:

By the same logic every command can be made evil:

for ( i=0;i<50;i++ );

  printf ( "Now counting: %d\n", i );

I disagree. My IDE would change the indentation, making it a lot easier to spot.

#14 bramz

    Valued Member

  • Members
  • PipPipPip
  • 189 posts

Posted 04 October 2005 - 09:39 AM

.oisyn said:

Well yes, you shouldn't use macro's for things that should actually be constants or inline functions. But to say that the whole construct of macro's are evil... Otherwise exceptions would be evil too, as you can use them for normal program flow [and more of such examples] :)

the "evil" thing is mostly used to make clear to former C programmers they should rethink how they use it. As such, macros, arrays, malloc, pointer casts are all evil ( http://www.parashift...e.html#faq-6.15 ).

But of all those "evil" things, the preprocessor really is! That's why we have habits like using ALL_CAPS for macro names and for macro names only. So that there's a red flag saying to be extra carefull when using them (and not ending up having your variable min replaced by a very very evil windows.h macro). The preprocessor is pure black magic. Very powerfull if used wisely (what we do all the time of course :) ), but so dangerous in the wrong hands ...

Bramz

PS: if exceptions are evil, then every language construct is evil, so no, they're not :P
hi, i'm a signature viruz, plz set me as your signature and help me spread :)
Bramz' warehouse | LiAR isn't a raytracer

#15 .oisyn

    DevMaster Staff

  • Moderators
  • 1810 posts

Posted 04 October 2005 - 04:01 PM

NomadRock said:

you will notice that i was not declared in the loop, and you will also notice that he uses printf and so is probably using C which doesnt even allow you to declare variables inside the loop.
C99 does :)
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#16 zavie

    Member

  • Members
  • PipPip
  • 91 posts

Posted 04 October 2005 - 05:44 PM

bramz said:

PS: if exceptions are evil, then every language construct is evil, so no, they're not :P

What about pure functional languages? ;-P

#17 Mihail121

    Senior Member

  • Members
  • PipPipPipPip
  • 1050 posts

Posted 04 October 2005 - 06:00 PM

That's why i like C! It's evil and not evil in the same time!

#18 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 04 October 2005 - 10:23 PM

the evil thing about the preprocessor is that it's just that: a preprocessor. it doesn't understand the language of the code, it doesn't care about it, and so does the compiler not care about the preprocessor parts.

they are individual, and both very different interpreting of the given code. the result: the preprocessor can change the way normally save code behaves, resulting in totally dangerous mess..

but thats why we love c and c++.. because somehow, it feels great to be part of that mess.. because we think we have it in our hands and control..
till the next crash :D
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

#19 .oisyn

    DevMaster Staff

  • Moderators
  • 1810 posts

Posted 05 October 2005 - 10:51 AM

bramz said:

PS: if exceptions are evil, then every language construct is evil, so no, they're not :)

Thank you for making my point :)
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#20 leblebi

    New Member

  • Members
  • Pip
  • 4 posts

Posted 05 October 2005 - 02:46 PM

In short, if you don't understand something (and in your case you don't even know the syntax let alone understand it), don't accuse it of being evil. Preprocessor is not an exception to this.

The comment about how indentation will help you spot it or etc. is not the issue here since I use vi to write C/C++ code.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users