- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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;
}
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
Link Copied
5 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 be able to optimize away.
There is a special option for Windows C++ to ignore scoping rules for cases somewhat like this, but it's often difficult to figure out what was meant when the same variable name is re-used in a shadowing mode.
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 be able to optimize away.
There is a special option for Windows C++ to ignore scoping rules for cases somewhat like this, but it's often difficult to figure out what was meant when the same variable name is re-used in a shadowing mode.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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;
}
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - Jennifer Jiang (Intel)
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - Stan
Quoting - Jennifer Jiang (Intel)
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
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page