Runtime storage Allocation:
Outline for the lecture:
-Virtual memory
-Allocation:
-static
-stack
-dynamic/heap(done in next lecture).
VIRTUAL MEMORY:
Each process has its own virtual memory. Each process has its own mapping from virtual memory to physical memory.
Process considers its memory as a continuous block whereas in reality it gets mapped sparingly in the main physical memory.
Advantages: privacy, protection, single program, idea of more memory
Disadvantages: complex, slower because a block of memory could be in the disk so needs to be loaded in.
STATIC ALLOCATION:
All the variables are given a fixed address at the beginning of the program. So the address stays static .
Advantage: simple,fast.
Disadvantages: wastes space( because a variable which may not me accessed during program execution gets space), waste after a variable has been accessed and will not be accessed any more, fixed size, no recursion.
Still used for: Code(executable),Data literal, global, static variables.
STACK ALLOCATION:(BASIC OPERATIONS PUSH AND POP)
-runtime stack
-local vars and "control info"(return address in the code) for procedures
-passing parameters and returning result.
-frame pointers are basically the dynamic link to the top of the stack.
Main(){
F();
G();
}
f(){
bool a;
int b;
}
g(){
int c;
char d;
}
D |
C |
Control info for g() |
Fp of f() while we are in g() G() is frame pointer points to this Location where we can find F() 's frame pointer which we saved here while pushing g(). |
B |
A |
Control info for d |
Main |
Push op:
save fp
fp<-top
top<-top+size(AR)
AR for activation record
Top for pointer to the top of the stack
Note: Nested blocks are complicated and slow.