Inside the loop, Outside the loop

Started by
6 comments, last by Kest 17 years, 6 months ago
I'm using Visual Studio C++ 7 compiler and IDE, and realized something strange. Well, I've known about it and known it was strange for a while, but I'm just now curious enough to ask what the deal is. With my IDE/compiler, with all warnings on, the programmer can do this..

for(int x=0; x<max; x++)
   Objects[x].Update();
for(int x=0; x<max; x++) // new int x
   Items[x].Update();
for(int x=0; x<max; x++) // new int x
   Maps[x].SkewTheMoon();
I guess this would imply that the variable is being declared in a special loop-scope. But then, the programmer can also do all of this in one go..

for(int x=0; x<max; x++)
   if( Objects[x].Dead )
      break;

if( x < max ) // using x from loop
   Kill( Objects[x] );

for(int x=0; x<max; x++) // A new int x
   Maps[x].SkewTheMoon();

for(x=0; x<max; x++) // Now lacking new dec
   Items[x].Update();
So what's the deal with this? I always try to avoid these strange situations, but I'm a little curious to hear how others percieve it. I'm also curious to understand what would happen behind the scenes. Does the previous x just vaporize when you declare a new one? Does the compiler create two (with different internal names)? Or does it just recycle the old one?
Advertisement
That's a MS extension, and it isn't supposed to work. Only the first form should compile, as the scope of a variable defined within the for statement ends after the loop. If you check your properties, there should be a compiler switch called "Force conformance in for scope" or something like that...you want that set to yes.

VS8 changes the default to being on, so the second code should no longer compile with a default project.

CM
I guess it's trying to be compatible with VC6, which was completely broken in this regard.
That version of the compiler didn't scope variables according to the ANSI C++ standard, specifically the 'for' scope. The latest version (VS2K5) is far better and the code you posted generates errors on the Kill and Update blocks.

If you want a more standard compliant compiler, get VS2K5 (the Express version is free). Otherwise you'll have to develop a coding standard to prevent this being an issue (e.g. no declaration inside a for control block).

Skizz
Quote:Original post by Skizz
If you want a more standard compliant compiler, get VS2K5 (the Express version is free). Otherwise you'll have to develop a coding standard to prevent this being an issue (e.g. no declaration inside a for control block).

While 2k5 is an obvious improvement, upgrading just for this seems a bit much [especially since, while nice, I would consider the Express editions a downgrade from 2k3 Standard]. 2k3 wasn't half bad in the standards compilance area, and this particular issue has an easy fix that doesn't rely on coding standards: just tell it to obey proper for-loop scoping rules.

CM
I'm still looking for that compiler option. Thanks for pointing it out [smile]
In VC2005 it's at Project->Properties->Configuration->C/C++->Language->Force Conformance In For Loop Scope. It's probably in roughly the same place in your version.

If you're using the command line to compile it's /Zc:forScope.
-Mike
Quote:Original post by Anon Mike
In VC2005 it's at Project->Properties->Configuration->C/C++->Language->Force Conformance In For Loop Scope. It's probably in roughly the same place in your version.

If you're using the command line to compile it's /Zc:forScope.

There it is. Thanks for directing me through all of the chaos.

This topic is closed to new replies.

Advertisement