In general, the execution of a program involves two kinds of memory: runtime sta
ID: 3829397 • Letter: I
Question
In general, the execution of a program involves two kinds of memory: runtime stack and heap. Local and parameter variables (in addition to the return addresses of sub-program calls) are stored on the stack, while heap-dynamic variables created during runtime and static variables are stored in the heap. (a) Explain why it is more expensive to create variables dynamically during runtime in the heap than those on the stack. (b) Explain why it is more expensive to access stack variables than static variables during runtime. (c) Variables can be categorized by their lifetimes. Accordingly, they are: static variables, stack-dynamic variables, explicit heap-dynamic variables, and implicit heap-dynamic variables. For each type of variables, state where each resides, give the mechanism behind how and when such variables are allocated memory from the operating system, and how and when such variables are deallocated.Explanation / Answer
a.
Dynamic variables are created on heap. And heap is far more complicated to maintain. Allocation and de-allocation of is a very complex task which needs to keep track of which part is free and which parts are allocated at any time. They involve usage of several complicated data structure like free-list. It needs to find the big enough block, split it, manage the book-keeping. Allocation from free storage usually involves asking requesting underlying OS for more process memory. Another major problem is fragmentation which can occur when there are lots of allocations and deallocations. It wastes memory and also makes it harder to find free spaces for large allocations.
Allocating in the stack is faster and easier, because it just requires changing of the stack pointer. Stack is limited and variables allocated on stack are guranteed to be destroyed once you leave the scope. Stack enforces LIFO pattern which makes it simple to keep track of blocks on it and freeing a block just requires adjusting the stack pointer.
b.
Static variables don't get re-allocated everytime when the function is called. Where as the stack varaibles needs to be allocated and de-allocated everytime and accessing requires changing of the stack pointer. So, therefore, static variables are a bit faster compared to stack variables.
c.
Static variable - It is allocated in static area during the start of the program. It's lifetime extends across the entire run of the program but the scope might be limited to the function or block they are declared in.
Automatic/Local variable - These are allocated in the stack area. They are allocated when the program flow enter the function or the block they are declared in and are destroyed when the scope is exited. Their scope is limited to the block or function they are declared in.
Dynamic Variables - Dynamic variables are allocated in the heap area of the memory. Lifetime of these variables begins when the memory is allocated using malloc() and ends when they are deallocated using free() or if the program exits first.