Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.
Announcements
The Intel sign-in experience is changing in February to support enhanced security controls. If you sign in, click here for more information.
7749 Discussions

warning #1491: definition from the enclosing scope variable ...

Stan
Beginner
285 Views
Hello everybody,

Either I'm blind already for something obvious or...

I have a very simple loop for a simple quick count -

size_t coord_size[10];
size_t new_max_total_size;

for( int a = 0, new_max_total_size = 0; a<10; a++ ) {
new_max_total_size += coord_size;
}

I get a warning : warning #1491:definition from the enclosing scope variable "new_max_total_size" ...

and new_max_total_size is simply UNDEFINED after passing the loop ! ( value 0xccccccc... )

At the moment I'm not convinced that the issue is due to the warning or due to problem with compiler - anybody did experience that? I did check that in debug mode only (putting variable to cout - knowing that sometime debug watch does not show correct values)

Regards,
Stan
0 Kudos
5 Replies
TimP
Black Belt
285 Views
Apparently, you intended something more like
new_max_total_size = 0;
for( int a = 0; a<10; a++ ) {
new_max_total_size += coord_size;
}

rather than defining a shadowing local new_max_total_size inside the for(), which the compiler might...

JenniferJ
Moderator
285 Views
Quoting - Stan
size_t coord_size[10];
size_t new_max_total_size;

for( int a = 0, new_max_total_size = 0; a<10; a++ ) {
new_max_total_size += coord_size;
}

I get a warning : warning #1491:definition from the enclosing scope variable "new_max_total_size" ...

and new_max_total_size is simply UNDEFINED after passing the loop ! ( value 0xccccccc... )

I used the Composer beta and got following warning which is more meanful:
[shell]warning #1420: declaration in for-initializer hides a declaration in the surrounding scope
 the hidden declaration is at line 6[/shell]

So the "new_max_total_size" is another new var within "for" scope.

ICL is compatible with VC in this. Even using /Zc:forScope-, icl or cldoes not give compile error but a warning only.
Stan
Beginner
285 Views

I used the Composer beta and got following warning which is more meanful:
[shell]warning #1420: declaration in for-initializer hides a declaration in the surrounding scope
 the hidden declaration is at line 6[/shell]

So the "new_max_total_size" is another new var within "for" scope.

ICL is compatible with VC in this. Even using /Zc:forScope-, icl or cldoes not give compile error but a warning only.

I had a feeling - it is (was?) agains the standard - as for-init-statement should be just executet once - and (in the past) was used by me to set also other vars... now - looking into C++ standard - I'm no longer sure... don't understand why - but let it be then.. have to control my code now...


TimP
Black Belt
285 Views
The code you presented would be rejected by C89. In C++ and C99 it defines a new variable in the scope of the for, and it is contrary to the standards for that one to be visible outside the scope, so it is good for the compiler to issue a warning (and traditional for the warning to be cryptic). Plenty of C++ programmers violate the standard as much as their favorite compiler will permit, without caring that the code will break in the hands of others or upon a compiler upgrade.
Thomas_W_Intel
Employee
285 Views
Quoting - Stan

I used the Composer beta and got following warning which is more meanful:
[shell]warning #1420: declaration in for-initializer hides a declaration in the surrounding scope
 the hidden declaration is at line 6[/shell]

So the "new_max_total_size" is another new var within "for" scope.

ICL is compatible with VC in this. Even using /Zc:forScope-, icl or cldoes not give compile error but a warning only.

I had a feeling - it is (was?) agains the standard - as for-init-statement should be just executet once - and (in the past) was used by me to set also other vars... now - looking into C++ standard - I'm no longer sure... don't understand why - but let it be then.. have to control my code now...



Stan,

Initializing several variables in hte for-init statement is still fine. As Jennifer already pointed out, you are (accidentially) declaring a new variable new_max_total_size of type int. The comma does not separate two statements but a list of variable declarations.

Kind regards
Thomas
Reply