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