The Stack & The Heap


I said that memory can be visualised of as one long list of bytes. Actually it is better to imagine it split into two sections. These sections are called The Stack and The Heap.

Some of you may have heard tales of these mysterious locations, such as “the stack grows down but the heap grows up”, or “there can be many stacks, but only one heap”. These sorts of things don’t matter much. Dealing with the stack and the heap in C can be complex, but it doesn’t have to be a mystery. In essence they are just two sections of memory used for two different tasks.

The Stack

building

The Stack • Like what you do with bricks.

The Stack is the memory where your program lives. It is where all of your temporary variables and data structures exist as you manipulate and edit them. Every time you call a function a new area of the stack is put aside for it to use. Into this area are put local variables, copies of any arguments passed to the function, as well as some bookkeeping data such as who the caller was, and what to do when finished. When the function is done the area it used is unallocated, ready for use again by someone else.

I like to think of the stack as a building site. Each time we need to do something new we corner off a section of space, enough for our tools and materials, and set to work. We can still go to other parts of the site, or go off-site, if we need certain things, but all our work is done in this section. Once we are done with some task, we take what we’ve constructed to a new place and clean up that section of the space we’ve been using to make it.

The Heap

storage

The Heap • U LOCK. KEEP KEY.

The Heap is a section of memory put aside for storage of objects with a longer lifespan. Memory in this area has to be manually allocated and deallocated. To allocate new memory the malloc function is used. This function takes as input the number of bytes required, and returns back a pointer to a new block of memory with that many bytes set aside.

When done with the memory at that location it must be released again. To do this the pointer received from malloc should be passed to the free function.

Using the Heap is trickier than the Stack because it requires the programmer to remember to call free and to call it correctly. If he or she doesn’t, the program may continuously allocate more and more memory. This is called a memory leak. An easy rule to avoid this is to ensure for each malloc there is a corresponding (and only one corresponding) free. If this can always be ensured the program should be handling The Heap correctly.

I Imagine the Heap like a huge U-Store-It. We can call up the reception with malloc and request a number of boxes. With these boxes we can do what we want, and we know they will persist no matter how messy the building site gets. We can take things to and from the U-Store-It and the building site. It is useful to store materials and large objects which we only need to retrieve once in a while. The only problem is we need to remember to call the receptionist again with free when we are done. Otherwise soon we’ll have requested all the boxes, have no space, and run up a huge bill.