Question: This code illustrates something that I think should be treated as bad practice, and elicit warnings from a compiler about redefining or masking a variable:

```
#include <iostream>

int *a;

int* f()
{
  int *a = new int;
  return a;
}

int main()
{
  std::cout << a << std::endl << f() << std::endl;
  return 0;
}

```

Its output (compiled with g++):

```
0
0x602010

```

I've looked at a couple references (Stroustrup and The Complete C++ Reference) and can't find anything about when and why this is allowed. I know that it's not within a single local scope, though.

When and why is this allowed? Is there a good use for this construct? How can I get g++ to warn me about it? Do other compilers squawk about it?

Answer: 
It's allowed so that you can safely ignore global identifier overriding. Essentially, you only have to be concerned with global names you actually use. 

Suppose, in your example, `f()` had been defined first. Then some other developer added the global declaration. By adding a name, `f()` which used to work, still works. If overriding was an error, then the function would suddenly stop working, even though it doesn't do anything at all with the newly added global variable.
A lot of languages allow this sort of thing.  

Usually (in relation to all languages) the most locally defined variable is the one you are referring too. Of the 20+ languages I have used this is very common.

Also most languages allow you to explicitly refer to the one in the outer scope.  

For example C++ alows you to specify the variable in global scope with the :: operator.

```
#include  <iostream>

int a = 5;
int main()
{
    int a = 6;

    std::cout << a << "\n" << ::a << "\n";
            // Local
                           // global
}

```