Variables
- 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
Comments
Post a Comment