Internals of how variable scoping works

O

Omari Norman

Greetings,

Take for instance the following code:

int main()
{
int x=5;
if (x == 5)
{
int y = 10;
}
return 0;
}

Of course x and y are in different scopes. What I am wondering is how
this works at a lower level. For instance, I know that typically
automatic variables are kept on the stack, and that function calls
cause the stack to be pushed down. I read a book that likened the
stack to index cards, with each function call getting its own index
card with its automatic variables.

So what happens when a new scope is entered, as here with the y=10
statement? I would think that the program does not create space for
the y variable when it first starts executing, because it doesn't
"know" whether that scope will ever get entered. So does the program
simply allocate additional room on the same frame in the stack when it
enters the new scope, or does it have to push the stack again to make
room for new variables?

I know this might vary from one implementation to another, but I just
want an idea how it generally works. I know my understanding is really
rudimentary so any pointers to useful sites or books would be great.
Thanks. --Omari
 
S

Stefan Ram

Omari Norman said:
int main()
{
int x=5;
if (x == 5)
{
int y = 10;
}
return 0;
}
So what happens when a new scope is entered, as here with the y=10
statement?

I assume that usually there will be a single »activation
record« (»stack frame«) for »main«, which always contains
objects for both x and y, although y might never be used.

The above code might be translated to the same object file as
if it would have been »int main(){}« by modern optimizing
compilers. So, the stack frame might actually not contain any
objects at all.
 
W

WDS

I know this might vary from one implementation to another, but I just
want an idea how it generally works.

I would guess that most compilers reserve space for automatic
variables at a procedural level because it is easy. A clever compiler
could save space by allocating the most that could possibly be needed
and then overlaying the variables in disjoint scopes.

I just tried something with gCC and MS Visual C++. gCC did reuse
stack space in disjoint scopes. Visual C++ reused it if compiled with
optimization but not if compiled for debug.
 
J

Jerry Coffin

[ ... allocating space for scopes inside of a function ]
I know this might vary from one implementation to another, but I just
want an idea how it generally works. I know my understanding is really
rudimentary so any pointers to useful sites or books would be great.

All of the implementation's I've seen allocate a stack frame once when
entering a function, and ensure that enough space is allocated for any
possible path through that function.
 
E

EventHelix.com

Greetings,

Take for instance the following code:

int main()
{
    int x=5;
    if (x == 5)
    {
        int y = 10;
    }
    return 0;

}

Of course x and y are in different scopes. What I am wondering is how
this works at a lower level. For instance, I know that typically
automatic variables are kept on the stack, and that function calls
cause the stack to be pushed down. I read a book that likened the
stack to index cards, with each function call getting its own index
card with its automatic variables.

So what happens when a new scope is entered, as here with the y=10
statement? I would think that the program does not create space for
the y variable when it first starts executing, because it doesn't
"know" whether that scope will ever get entered. So does the program
simply allocate additional room on the same frame in the stack when it
enters the new scope, or does it have to push the stack again to make
room for new variables?

I know this might vary from one implementation to another, but I just
want an idea how it generally works. I know my understanding is really
rudimentary so any pointers to useful sites or books would be great.
Thanks. --Omari

Compilers typically maintain one stack frame per function call.
Multiple scopes will also be managed within the stack frame.

The following link explains stack frames in detail:

http://www.eventhelix.com/RealTimeMantra/basics/CToAssemblyTranslation.htm
 
T

Tim Slattery

Omari Norman said:
Greetings,

Take for instance the following code:

int main()
{
int x=5;
if (x == 5)
{
int y = 10;
}
return 0;
}
So what happens when a new scope is entered, as here with the y=10
statement?

It would be implementation dependent, but IMHO pushing something on
the stack usually involves nothing more than manipulating a pointer.
So the program would push space for y when it enters the if block, and
pop it when it leaves. This would take *very* little time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top