You are on page 1of 7

CP2003 Lecture Notes - Dynamic Storage

These lecture notes are based on chapter 3 in ``Programming Language Concepts and Paradigms'' by David Watt, and chapters 6 and 9 in ``The Anatomy of Programming Languages'' by Alice Fischer and Frances Grodzinsky.

Heap vs. Stack


There are two types of dynamic storage which need to co-exist in memory. They both grow and shrink throughout program execution. A visual depiction of this runtime storage looks like this:

Heap Storage o generally used as storage for object with an unrestricted lifetime o maintains a freelist - list of free space o on allocation - memory manager finds space and marks it as used best-fit worst-fit first-fit o on deallocation - memory manager marks space as free o memory fragmentation - the memory fragments into small blocks over the lifetime of program o garbage collection - coalesce fragments, possibly moving objects Stack Storage o storage for objects with nested lifetimes (such as function parameters and local variable) o very useful in recursion o central concept is the stack frame (also called activation record) visual depiction of a frame:

o o o

parameters: function parameters return address: where to begin execution when function terminates dynamic link: pointer to caller's stack frame static link: pointer to lexical parent (for nested functions) return value: where to put the return value local variables: the functions local variables also a local work space which is for temporary storage of results function is called - push stack frame function is exited - pop stack frame visual depiction of stack calls:

int x; /* static storage */ void main() { int y; /* dynamic stack storage */ char *str; /* dynamic stack storage */ str = malloc(100);/* allocates 100 bytes of dynamic heap storage */ y = foo(23); free(str); /* deallocates 100 bytes of dynamic heap storage */ } /* y and str deallocated and stack frame popped */ int foo(int z) { char ch[100]; if (z == 23) foo(7); return 3; } /* z is dynamic stack storage */ /* ch is dynamic stack storage */ /* z and ch are deallocated as stack frame is popped, 3 is put on top of the stack */

At the start of the program:

after the first call to foo:

After the second call to foo:

Dead Objects
storage objects in stack storage die (are deallocated) when the stack frame is popped. storage objects in heap storage can be explicitly killed (as in C), but in other languages are implicitly killed when they are no longer referenced. For example: recall that the freelist is a list of free areas in heap storage. Recall also that memory for objects is allocated from the freelist as required, and returned to the freelist upon deallocation. Garbage collection is the process of coalescing the heap storage areas pointed to by the freelist. it is also possible to end up with pointers to dead objects. These pointers are called dangling references. For example:

Persistent Variables
A persistent variable is a variable that lives beyond the execution of a program. Example is a file is programming languages that allow file access:
{--- Pascal Example} program pants(a_file_name) variable} ... end. {--- a_file_name is a persistent file

But why should files be different than other types of variables? This is a violation of the type completeness principle. Research is underway to add persistence to programming languages (e.g. persistent Java). http://www.cs.jcu.edu.au/Subjects/cp2003/1998/morestorage.html