Comp 110 The Stack vs The Heap

What is "The Stack"?

The stack is the program's "working space." It consists of a bunch of 'frames' that will correspond with each function call. 

Every stack will always have a Globals Frame, containing the global variables of the program (anything declared outside a function! Functions are also considered global variables!

Each following frame added to the stack corresponds with a function call, and will contain: the name of the function definition, a list of the variable names/boxes holding their bound values (primitive values will be stored in the stack frames).

When a function is finished executing, that frame is "popped" (erased) off the stack.

Variables in the stack will never point to other things on the stack!

To make sense of this in COMP110, we use environment diagrams!

What is "The Heap"?

The heap is essentially dynamic memory. Variables (that are not primitive values) in the stack point to functions, arrays, and objects in the heap! As elements in arrays and properties of objects change, the heap also changes! 

This is a broad conceptual overview of what the stack and the heap are for! Reference the other pages under "The State of the Program" for specifics on how to create and manage environment diagrams.