Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Internals of how variable scoping works (http://www.velocityreviews.com/forums/t622065-internals-of-how-variable-scoping-works.html)

Omari Norman 06-24-2008 09:21 PM

Internals of how variable scoping works
 
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

Stefan Ram 06-24-2008 09:36 PM

Re: Internals of how variable scoping works
 
Omari Norman <OriginalOmari@gmail.com> writes:
>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.



WDS 06-24-2008 09:39 PM

Re: Internals of how variable scoping works
 
On Jun 24, 4:21*pm, Omari Norman <OriginalOm...@gmail.com> wrote:
> 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.

Jerry Coffin 06-24-2008 09:46 PM

Re: Internals of how variable scoping works
 
In article <fa707fd9-d634-41b1-8db9-ae9688b076d5
@c58g2000hsc.googlegroups.com>, OriginalOmari@gmail.com says...

[ ... 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.

--
Later,
Jerry.

The universe is a figment of its own imagination.

EventHelix.com 06-25-2008 02:33 AM

Re: Internals of how variable scoping works
 
On Jun 24, 5:21*pm, Omari Norman <OriginalOm...@gmail.com> wrote:
> 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/RealTimeMa...ranslation.htm

--
http://www.EventHelix.com/EventStudio
Sequence diagram based embedded systems design tool


Tim Slattery 06-25-2008 12:53 PM

Re: Internals of how variable scoping works
 
Omari Norman <OriginalOmari@gmail.com> wrote:

>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.

--
Tim Slattery
Slattery_T@bls.gov
http://members.cox.net/slatteryt


All times are GMT. The time now is 10:27 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.