Variable Classification and Memory Organization
Variable Classification
- There are 3 kinds of variables, based on how and where they are declared:
- Package level variables - named in a declaration that is not in a procedure or function
Example:
package BigNumPkg is
...
private
Size : constant Positive := 50;
type BigNum is array(0..Size-1) of Integer;
- Local variables -
named in a declaration that is in a procedure or function, or in a declare block
Example:
with bignum;
procdure foo is
b : bignumpkg.bignum; -- Local
begin
...
declare
f : Integer; -- Local
...
- Anonymous variables - NOT named in a declaration
- Access with a pointer
- Note that the pointer to the anonymous variable is itself
frequently a local variable
- Kind is not a standard technical term, but it is convenient for our use (eg what kind of variable
is it? A local variable)
Variable Allocation and Deallocation
- Variable allocation refers to when space in memory is allocated (ie set aside) for the variable
- Different kinds of variable have different allocation and deallocation protocols
- Package level variables are allocated/deallocated when the program begins/ends
- Local variables are allocated/deallocated each time the routine begins/ends execution following a stack protocol
- When the procedure is called, the locals are stacked and when it exits, they are popped
- Example:
- Procedures A, B, and C have locals X, Y, and Z, respectively.
- if A calls B which calls C, then C's
locals are on top of the stack and A's locals are on the bottom of the
stack
- Anonymous variables are allocated when explicitly created by
the program and deallocated when explicitly destroyed by the program
- When known:
- Allocation of package level are can be determined at compile
time (ie it is static)
- Allocation of locals and anonymous are not known until runtime (ie it is dynamic)
Memory Organization
- Memory is divided into three areas: static, stack, heap (not related to
heap sort or heap data structure)
- Each area holds a different kind of variable
- static: holds package level
variables.
(In C and C++, variables declared as static are declared here.)
- stack: for local variables
- heap: for anonymous variables The pointer
variable is usually a local variable
- Note that memory can be allocated many times to a given variable
name. To be precise, we use the term variable to refer to the
area of memory, not the name, but usually we don't distinguish between
the two.
Java Example
- In Java, objects are allocated on the heap and local
variables on the stack:
class Foo{
void m(){
int l = 3;
Goo g = new Goo();
}
}
class Goo{
int x = 4;
int y = 5;
}
- Java allocates static variables on
the heap, not in the static area. As far as I know, Java only uses the static area for the runtime environment.
Physical Memory Organization
- different from picture given earlier since memory can also be
modeled as a single long area:
- static starts at address 0
- the heap is next
- the stack starts at the other end of memory
- the stack and the heap grow toward each other
- the memory manager keeps the stack and heap from meeting
- this explains why stack addresses decrease as elements are
added to the stack