CIS505 Weds. Sept. 17      SAMPLE SOLUTION

Draw the stack and heap  when this program reaches the BREAKPOINT:

var init = 0;
class Scorer = struct 
                var score = init;
                proc add(n) { score = score + n;  
                              //BREAKPOINT HERE
                            }            
               end;
var a = new Scorer;
var b = new Scorer;
a.add(3);

=================================

Stack: [h0, h6] (top)

Heap:  {  h0: { init: 0,           h1: {code for Scorer, h0}
                Scorer: h1,
                a: h2,
                b: h4, 
                parentns: nil }
 
          h2: { score: 3,          h3: {code for add, h2}
                add: h3 
                parentns: h0 }

          h4: { score: 0,          h5: {code for add, h4}
                add: h5 
                parentns: h0 }

          h6: { n: 3,
                parentns: h2 }
        }

==================================

It looks inefficient to save both  h3 and h5  in the heap,
but they are necessary because we can write the following:

var p = b.add;   // assigns  h4  to  p
p(7);            // calls  b's  add,  which needs the link to  h4
          
The above is an accurate drawing of memory layout for a Javascript
or Ruby or Python interpreter.  Smalltalk uses a similar layout.
Please see Section 6.2.3 to see the memory layout produced by a Java compiler.