🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Untitled

posted in DruinkJournal
Published October 23, 2007
Advertisement
Reasons why macros are bad, #4127:
#define foo(x) for(int i=0; i

Well, I thought that would work as "expected", but apparently not. If you do the following:
int i = 42;foo(i);

You'll never end up with blah() getting called, because the preprocessor will expand the macro to:
int i = 42;for(int i=0; i

Oh well. That's what I get for using macros I guess...
Previous Entry Untitled
Next Entry Untitled
0 likes 7 comments

Comments

mldaalder
But shouldn't the int i in the for-loop be the nearest lexical scope?

So like this:
int i = 42;
// i = 42
for(int i=0;....;...)
{
 // i starts from 0 and changes
}
// i = 42 again because of scopes


If I word it differently, isn't the i in the for-loop not a different i than outside the for-loop?

Or are we talking about (ANSI) C here?
In which case it should say something about not allowing variable declarations there.
October 23, 2007 10:52 AM
Jotaf
In which case it should warn you about a variable redefinition :)
October 23, 2007 11:24 AM
Pouya
Rename your variable to _LOL_inner_macro_s_loop_counter_please_don_t_make_a_local_variable_with_this_name_anywhere_18y32irh329
October 23, 2007 01:07 PM
Evil Steve
Quote: Original post by mldaalder
But shouldn't the int i in the for-loop be the nearest lexical scope?

So like this:
int i = 42;
// i = 42
for(int i=0;....;...)
{
 // i starts from 0 and changes
}
// i = 42 again because of scopes


If I word it differently, isn't the i in the for-loop not a different i than outside the for-loop?

Or are we talking about (ANSI) C here?
In which case it should say something about not allowing variable declarations there.
Yes. My example is shit. I've edited it now [smile]
October 23, 2007 04:31 PM
Evil Steve
Quote: Original post by mldaalder
But shouldn't the int i in the for-loop be the nearest lexical scope?

So like this:
int i = 42;
// i = 42
for(int i=0;....;...)
{
 // i starts from 0 and changes
}
// i = 42 again because of scopes


If I word it differently, isn't the i in the for-loop not a different i than outside the for-loop?

Or are we talking about (ANSI) C here?
In which case it should say something about not allowing variable declarations there.
Sorry, this is C++ (Well, CodeWarrior's version of it anyway). The problem is that the macro expansion just causes it to substitute "x" for "i". i is never less than i, so the loop never executes.

Quote: Original post by Jotaf
In which case it should warn you about a variable redefinition :)
Ah, but the variable in the for() loop scope hides the i variable in the outer scope. Because they're at two different scopes, it's all legal and above board.

Quote: Original post by Pouya
Rename your variable to _LOL_inner_macro_s_loop_counter_please_don_t_make_a_local_variable_with_this_name_anywhere_18y32irh329
Actually, the real code was a flood fill algorithm, and the macro was a PutPixel() function (I don't trust CodeWarrior to actually inline anything I tell it to, it has no choice if it's a macro). The loop in the macro was for(int x=0; x<blah; ++x) and a similar loop for y. I just changed the loop vars to xxx and yyy.
So I fully expect to be pulled up on that at the next code review for a) using bizzarely named variables and b) using macros in the first place.
October 23, 2007 04:37 PM
Evil Steve
Quote: Original post by mldaalder
But shouldn't the int i in the for-loop be the nearest lexical scope?

So like this:
int i = 42;
// i = 42
for(int i=0;....;...)
{
 // i starts from 0 and changes
}
// i = 42 again because of scopes


If I word it differently, isn't the i in the for-loop not a different i than outside the for-loop?

Or are we talking about (ANSI) C here?
In which case it should say something about not allowing variable declarations there.
Sorry, this is C++ (Well, CodeWarrior's version of it anyway). The problem is that the macro expansion just causes it to substitute "x" for "i". i is never less than i, so the loop never executes.

Quote: Original post by Jotaf
In which case it should warn you about a variable redefinition :)
Ah, but the variable in the for() loop scope hides the i variable in the outer scope. Because they're at two different scopes, it's all legal and above board.

Quote: Original post by Pouya
Rename your variable to _LOL_inner_macro_s_loop_counter_please_don_t_make_a_local_variable_with_this_name_anywhere_18y32irh329
Actually, the real code was a flood fill algorithm, and the macro was a PutPixel() function (I don't trust CodeWarrior to actually inline anything I tell it to, it has no choice if it's a macro). The loop in the macro was for(int x=0; x<blah; ++x) and a similar loop for y. I just changed the loop vars to xxx and yyy.
So I fully expect to be pulled up on that at the next code review for a) using bizzarely named variables and b) using macros in the first place.
October 23, 2007 04:40 PM
ArchWizard
Or use an inline function.
October 27, 2007 11:15 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement