3

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?

asked Jan 30, 2015 at 22:42
2
  • I'm new to Stackexchange, I didn't really know in which section to put this question. Commented 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. Commented Jan 31, 2015 at 17:14

2 Answers 2

5

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.

answered Jan 30, 2015 at 22:58
3
  • So there are two different types of stacks? Commented Jan 30, 2015 at 23:08
  • 3
    There 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... Commented Jan 30, 2015 at 23:14
  • 1
    Yes, 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]. Commented Jan 30, 2015 at 23:16
1

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.

answered Jan 30, 2015 at 22:49
5
  • 2
    The stack has nothing to do with variable names in the source code. Commented Jan 30, 2015 at 22:51
  • Are the methods also stored on the stack? Or just the value types inside the methods? Commented 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. Commented Jan 30, 2015 at 23:00
  • Thanks, I added more description above to address the "why" and not just the "how". Commented Jan 30, 2015 at 23:01
  • 2
    Removed my downvote, but I think explaining it with implementation details (call stack and frame offsets) is not very helpful. Commented Jan 30, 2015 at 23:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.