473,394 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
Jun 27 '08 #1
4 1103
WDS
On Jun 24, 4:21*pm, Omari Norman <OriginalOm...@gmail.comwrote:
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.
Jun 27 '08 #2
In article <fa707fd9-d634-41b1-8db9-ae9688b076d5
@c58g2000hsc.googlegroups.com>, Or***********@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.
Jun 27 '08 #3
On Jun 24, 5:21*pm, Omari Norman <OriginalOm...@gmail.comwrote:
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

Jun 27 '08 #4
Omari Norman <Or***********@gmail.comwrote:
>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
Sl********@bls.gov
http://members.cox.net/slatteryt
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Michael G | last post by:
I am a little surprised that the following that $x is visible outside of the scope in which it is (?)defined(?) (not sure if that is the correct term here). I am trying to find where in the php...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
1
by: Luxore | last post by:
Hello, I am trying to create threaded python project and I'm running into some weird Python variable scoping. I am using the "thread" module (I know, it's old and I should be using...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
5
by: John Kelsey | last post by:
Back in the "old" C/C++ days, I used to declare static variables inside functions. Something like... // just a silly example to demonstrate the technique int foo(void) { static int NextVal =...
8
by: Florian Daniel Otel | last post by:
Hello all, As the subject says, I am a newcomer to Python and I have a newcomer question wrt namespaces and variable scope. Obviously, I might be missing smth obvious, so TIA for the patience...
9
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13,...
10
by: John Passaniti | last post by:
(Note: This is not the same message I posted a week or so ago. The problem that prevented my previous attempt to work was a silly error in the template system I was using. This is a problem...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.