I was wondering why is it possible that we can use the same name for local variable in different scopes?
Like we can have two methods which both can have variable with the same name.
What makes this possible and what happens behind the scenes?
-
I'm new to Stackexchange, I didn't really know in which section to put this question.Akainu– Akainu2015年01月30日 22:42:12 +00:00Commented Jan 30, 2015 at 22:42
-
From one point of view, scoping exists SO we can use the same name in different places without conflicting; this lets us pick the best name for a variable more easily and, more importantly, ont keep the entire program or file in our head at once.Weaver– Weaver2015年01月31日 17:14:50 +00:00Commented Jan 31, 2015 at 17:14
2 Answers 2
When the compiler enters a "new scope", new variables within that scope are stored for that scope, and when the scope "disappears", those variables disappears. This is, normally, done in the form of some sort of stack/list type, which in turn holds the variable names and their type.
So, if we have:
int x;
void func()
{
float x, y;
int a;
}
the stack will have level 0 with {x, int}
, and level 1 with {x, double}, {y, double}, {a, int}
.
I've written my own pascal compiler in C++ (using LLVM to do the actual code generation), and here's my "name-stack":
https://github.com/Leporacanthicus/lacsap/blob/master/stack.h
It uses the StackWrapper to keep track of the number of levels.
This should not be confused with the fact that a stack is used for nesting calls to functions, and storing the local variabls - that's a stack in the runtime environment, and whilst both conceptually are similar, the stack in the compiler is there to track different scopes, the one in the runtime keeps track of calls to functions, arguments to functions and local variables.
-
So there are two different types of stacks?Akainu– Akainu2015年01月30日 23:08:32 +00:00Commented Jan 30, 2015 at 23:08
-
3There are a LOT of different kinds of stacks. CPU runtime, compiler interpretation, at least one UI widget, piles of dishes in the sink, tall shelves in a library...DougM– DougM2015年01月30日 23:14:41 +00:00Commented Jan 30, 2015 at 23:14
-
1Yes, there are several "places" where you can use the term stack in the computer environment, including for example "software stack" [the OS-drivers-UI-application "stack"], "network stack" [look up the OSI model]. My compiler actually has several stacks for keeping track of different types of things during compilation, although they are conceptually the same type aside from what they contain [Variables, Types, Constants and a few other internal things].Mats Petersson– Mats Petersson2015年01月30日 23:16:18 +00:00Commented Jan 30, 2015 at 23:16
Why can we use the same name for local variable in different scopes?
Each method has its own set of local variables. The compiler converts these local variable names, in essence, to per-method unique numbers (stack frame offsets). After compilation the names of local variables are lost (disregarding some of the newer reflection APIs).
Local variables only exist while the method is being executed and are reset each time the method is invoked.
What makes this possible and what happens behind the scenes?
Local variables are stored on the "stack". Thus, each call to a method gets its own copy of local variables. As the methods return the space allocated on the stack is released.
This stack is also the key reason that method calls can even be recursive, where a method calls itself. Each call gets its space on the stack.
Further search for terms like "stack" and "local variable" may let you find more.
-
2The stack has nothing to do with variable names in the source code.user7043– user70432015年01月30日 22:51:59 +00:00Commented Jan 30, 2015 at 22:51
-
Are the methods also stored on the stack? Or just the value types inside the methods?Akainu– Akainu2015年01月30日 22:55:29 +00:00Commented Jan 30, 2015 at 22:55
-
The values for the local variables are stored on the stack. The methods themselves are stored with the loaded class.Dan Armstrong– Dan Armstrong2015年01月30日 23:00:38 +00:00Commented Jan 30, 2015 at 23:00
-
Thanks, I added more description above to address the "why" and not just the "how".Dan Armstrong– Dan Armstrong2015年01月30日 23:01:17 +00:00Commented Jan 30, 2015 at 23:01
-
2Removed my downvote, but I think explaining it with implementation details (call stack and frame offsets) is not very helpful.user7043– user70432015年01月30日 23:04:05 +00:00Commented Jan 30, 2015 at 23:04