• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Variable "shadowing"?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Variable "shadowing"?


  • Subject: Re: Variable "shadowing"?
  • From: Chris Espinosa <email@hidden>
  • Date: Fri, 18 Mar 2005 11:58:03 -0800

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
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Variable "shadowing"? (From: Mark Dawson <email@hidden>)

  • Prev by Date: Re: Get info not the same as documentation?
  • Next by Date: Include paths, and case sensitivity
  • Previous by thread: Variable "shadowing"?
  • Next by thread: Re: Variable "shadowing"?
  • Index(es):
    • Date
    • Thread