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:
- Literal values are copied, even when passed in as parameters. In other words, a number, boolean, or string will never be pointing to somewhere on the heap. Check out pages on Primitive Parameters and Local Primitives.
- 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. Check out pages on Reference Parameters and Local References.
- Name resolution: If you are looking for a variable or function in your frame and cannot find it, look in its parent! (We will talk about global variables later)
- 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.