Re: Porting from VC6 to VS2005

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"AVee" <AVee@xxxxxxxxxxxxxxxx> ha scritto nel messaggio news:%23GLBX7KIJHA.4240@xxxxxxxxxxxxxxxxxxxxxxx

In the mean time, I am interested in the loop scoping differences. Do you have a link to some info that you could quickly include?

I think that David refers to this:

Sample code snippet:

for ( int i = 0; i < N; i++ )
{
....
}

printf( "i = %d\n", i );

In VC6, the loop index 'i' has a scope outside the for loop, and you can use 'i' value like in printf above.
i.e. the above code is like this:

int i;
for ( i = 0; i < N; i++ )
...
printf( "i = %d\n", i );

Instead, new C++ standard considers 'i' as having a scope inside for loop, such that the initial for code corresponds to something like this:

{
int i;
for ( i = 0; i < N; i++ )
...
}
// You can't use 'i' here
printf( "i = %d\n", i ); // <<-- error

The new C++ standard allows you to write code like this:

for ( int i = 0; i < N; i++ )
...

for ( int i = 0; i < M; i++ )
...

for ( int i = 0; i < L; i++ )
...

because 'i' does not exist anymore when each of the for loop terminates.
Instead, you could not write code like this with VC6: you hade 'i' redefinition error.

HTH,
Giovanni


.



Relevant Pages

  • Re: About String
    ... My idea is that every statement list is a scope. ... You declare something inside a loop body, ... The same sense as declare blocks. ... SCOPE [DECLS] STATEMENTS END-SCOPE ...
    (comp.lang.ada)
  • Re: Which scope for variables being used in a loop?
    ... can't be GC'd until the scope they are declared in exist. ... inside of the loop. ... overlay local allocations in another loop. ... declaration itself, such that the pointer-value of the variable is ...
    (comp.lang.java.programmer)
  • Re: Scope Best Practice
    ... creating a new object versus the cost of maintaining it in memory?" ... the object is ready for GC once the loop is complete ( ... even if it remains in scope because it is not being referenced anywhere ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: NAG, Sun, Compaq possible f95 bug
    ... Range of a DO loop and scope are different. ... that wasn't mean to say that Lahey was nonconforming ... and thus the compiler is allowed to ...
    (comp.lang.fortran)
  • Re: Dereferencing Hash of Arrays
    ... I guess it has the opposite effect. ... is to always declare variables in the shortest scope possible. ... Change that while loop to: ... iteration begins, %data will be destroyed and your next iteration will ...
    (comp.lang.perl.misc)