On Mar 18, 2005, at 11:38 AM, Mark Dawson wrote: What does it mean to turn on variable shadowing?
I'm getting errors like:
declaration of "index" shadows a global implementation - (void)insertGraphic:(Draw2D_BaseGraphic *)graphic atIndex:(unsigned)index
/usr/include/string.h:107: warning: shadowed declaration is here char *index(const char *, int);
It means you have a local variable with the same identifier as a global variable. The compiler is assuming you want to use the local, but is letting you know in case you really intended to use the global.
In this case, the system headers for most modern Unices define a C function in the global scope—char* index(const char *, int)—and that will caluse a warning when you define a formal parameter—atIndex:(unsigned) index—with the same identifier.
To quote the gcc reference:
-Wshadow This option warns about the redeclaration of a variable name in a scope where it has already been declared. This is referred to as variable shadowing, and causes confusion about which occurrence of the variable corresponds to which value. The following function declares a local variable y that shadows the declaration in the body of the function: double test (double x) { double y = 1.0; { double y; y = x; } return y; } This is valid ANSI/ISO C, where the return value is 1. The shadowing of the variable y might make it seem (incorrectly) that the return value is x, when looking at the line y = x (especially in a large and complicated function). Shadowing can also occur for function names. For example, the following program attempts to define a variable sin which shadows the standard function sin(x). double sin_series (double x) { /* series expansion for small x */ double sin = x * (1.0 - x * x / 6.0); return sin; } This error will be detected by the -Wshadow option.
If you don't want the shadowing warning, set -Wnoshadow. But the best thing to do is to pick a less generic identifier name than "index" for your formal parameter.
Chris |