• Popular Now

• 12
• 12
• 9
• 10
• 13

Archived

This topic is now archived and is closed to further replies.

Variable declarations

This topic is 5617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

This may seem like an odd question but what are the effects of placing, say, the int keyword in front of a variable? Don''t worry, I know what it does program-wise But supposing I had a piece of code like this...
for(i = 0; i < 10; i++)
{
int Temp = (some complex equation using i, probably)
...
} 
is it any different whatsoever from...
int Temp;

for(i = 0; i < 10; i++)
{
...
} 
? Execution-wise, I know they''re the same. I haven''t been coding in c/c++ all this time without knowing how to declare variables ^^; But does it, say, declare the variable the first time the int declaration is encountered and then skip it entirely the next times, or does the compiler (MSVC++ 6.0, in my case) simply compile to declare everything before it starts executing code/functions/whatnot anyhow, meaning the position of the int declaration would be completely irrelevant? (and again, yes, I know it doesn''t change a thing at run-time, and that the contents of the variable aren''t affected ;P)

Share on other sites
the scope or life span of temp is within the brackets {}. So in the first one the varriable is declared and undeclared each time where as its only done once in the second one.

Share on other sites
Scope of variable declaration also affects stack depth. Depending on your compiler, if you''re assidious about declaring variables in the closest depth necessary, you can actually reduce your program''s working set.

Share on other sites
So the second case is less of a waste than the first case? Allright, thanks :D

I have been enlightened.

Share on other sites
No no no!

Don''t think that the second way is more efficient. They''ll both run at the same speed. A variable is "forgotten" when it goes out of scope, but that operation is free in terms of execution speed. The first way is much better programming practice, if the variable you''re declaring only has relevance within the loop.

Zyroth, you should be ashamed of yourself.

Don''t listen to me. I''ve had too much coffee.

Share on other sites
Gah, I''m confused ^^;

Ok, supposing the variable only has relevance within the loop in a function called multiple times during the main game loop (ie, say, something that would calculate the color of a pixel; for a ray-tracer, for instance) would either case have any effect whatsoever on speed? (Yeah, I know, at most a second or so''d be shaved off a 4-5 minute long calculation but I''m picky ^-^)

The way I see it, if the compiler creates the executable in such a way it would reserve space for all variables at the start of the function by scanning the function in advance for declarations, therefore ignoring any variable declaration as it executes the code (as their memory has already been allocated and all) then less work would be done. But if that ''int'' makes it try to reserve space for, in my example, Temp, it would only be trying to allocate space and reserve memory for it but giving up since it already has been reserved, then that''s like having a conditional branch ("is memory ''x'' already reserved?" or whatnot; not sure how it does this exactly ) being performed every time for no reason.

Or does variable declaration, basically, work some other way than that...?

Share on other sites
For simple types, there will be no difference (apart from the scope, of course). If you have a more complex type, such as a class with a constructor and destructor, those will get called every iteration of the loop and that''s much more inefficient.

As for reserving space:

  void foo (){  for (...)  {    int bar [100000];  }  while (...)  {    int bar [150000];  }}

I certainly hope the compiler will only use approx 600K of stack space and not 1000K, which would happen if it just added up all the local variables.