Internals of how variable scoping works

Discussion in 'C++' started by Omari Norman, Jun 24, 2008.

  1. Omari Norman

    Omari Norman Guest

    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
    Omari Norman, Jun 24, 2008
    #1
    1. Advertising

  2. Omari Norman

    Stefan Ram Guest

    Omari Norman <> 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.
    Stefan Ram, Jun 24, 2008
    #2
    1. Advertising

  3. Omari Norman

    WDS Guest

    On Jun 24, 4:21 pm, Omari Norman <> 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.
    WDS, Jun 24, 2008
    #3
  4. Omari Norman

    Jerry Coffin Guest

    In article <fa707fd9-d634-41b1-8db9-ae9688b076d5
    @c58g2000hsc.googlegroups.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.
    Jerry Coffin, Jun 24, 2008
    #4
  5. On Jun 24, 5:21 pm, Omari Norman <> 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/RealTimeMantra/basics/CToAssemblyTranslation.htm

    --
    http://www.EventHelix.com/EventStudio
    Sequence diagram based embedded systems design tool
    EventHelix.com, Jun 25, 2008
    #5
  6. Omari Norman

    Tim Slattery Guest

    Omari Norman <> 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

    http://members.cox.net/slatteryt
    Tim Slattery, Jun 25, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. NevilleDNZ
    Replies:
    9
    Views:
    439
    NevilleDNZ
    Aug 16, 2006
  2. Jobs Gooogle
    Replies:
    2
    Views:
    471
    Patricia Shanahan
    May 11, 2007
  3. Jobs Gooogle
    Replies:
    1
    Views:
    312
    Victor Bazarov
    May 10, 2007
  4. Jobs Gooogle

    .Net VC++ Java C++ Windows Internals Unix Internals

    Jobs Gooogle, May 10, 2007, in forum: C Programming
    Replies:
    0
    Views:
    351
    Jobs Gooogle
    May 10, 2007
  5. Jobs Gooogle
    Replies:
    0
    Views:
    121
    Jobs Gooogle
    May 10, 2007
Loading...

Share This Page