Comp 110 Environment Diagram Overview

Environment Diagrams

Environment diagrams are a way for us to trace through the execution of a program and keep track of the information we have stored on the stack and the heap at any given point. For a review of the differences between the stack and heap, check out this page.

Here are some general rules of thumb when drawing out your own environment diagrams:

  1. Globals first! Add your globals to the stack/heap before you start adding on more stack frames.
  2. Literal values are copied, even when passed in as parameters. In other words, a number, boolean, string will not ever be pointing to somewhere on the heap.
  3. Never draw arrows from a stack frame to another stack frame! If a reference type is declared in some frame, it is referring to somewhere on the heap and that is where the arrow should be drawn to.
  4. Name resolution: If you are looking for a variable or function in your frame and cannot find it, look in its parent , which for our purposes is always the global frame. (not necessarily the frame directly above)
  5. Once a function returns, erase the frame from your stack. Arrows pointing to the items on heap are erased but the value on the heap remains, even if it has no arrows pointing to it anymore.