Sign in to follow this  

Inside the loop, Outside the loop

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I guess it's trying to be compatible with VC6, which was completely broken in this regard.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this