9 2 Memory Layout 9 3 Understanding how memory is arranged and laid out is essential to becoming a good programmer Intro to Programming II Tracing Helps you understand Runtime errors in a program box and arrow tracing is a high level way to understand the execution of a program Equality tests Constructors Chris Brooks Box and Arrow Tracing Tracing by hand is a useful way to understand what s going on Boxes represent memory cells Arrows represent pointers to memory cells Differences between primitives and references Department of Computer Science University of San Francisco Two models Box and arrow tracing Runtime tracing Department of Computer Science University of San Francisco p 1 9 4 Box and Arrow Tracing Department of Computer Science University of San Francisco p 2 9 5 Example Primitives are represented by boxes Data is stored directly int x 4 Object references are represented by boxes and arrows Student z new Student Multiboxes are used to represent multiple slots within an z setName bob Department of Computer Science University of San Fra 9 6 Example x is a primitive Data is stored in a box String y hello world y and z are object references y and z are pointers to memory locations object Department of Computer Science University of San Francisco p 4 Department of Computer Science University of San Francisco p 5 Department of Computer Science University of San Fra 9 7 Tracing practice 9 8 Perform box and arrow tracing of the following program Create a multi box for every object created with new one cell for every data member Point an arrow from the object reference cell to the multi box representing the object When a method is called create boxes for each parameter and local variable Include a box for this Be sure and put the actual parameter values into the new boxes representing the formal parameters More detailed tracing 9 9 Box and arrow tracing is nice but too high level sometimes Doesn t let us keep track of how memory is allocated sizes or where things are stored Run time environment The run time environment refers to the way in which memory is used arranged Memory is arranged as a sequence of addresses We will also do more detailed tracing of programs to see what s actually happening Each address refers to a word in memory We can break the runtime environment into four sections Program code Where the program itself resides Global data area Global and static data is stored here Run time stack This contains an activation record for each method that is called Heap Dynamically allocated data with new or malloc is stored here Department of Computer Science University of San Francisco p 7 9 10 Activation Records Department of Computer Science University of San Francisco p 8 9 11 Symbol table 9 12 An activation record sets a context for a method s execution The symbol table is responsible for mapping variable names to It contains Space for all parameters including this a pointer to the This is how the Java interpreter knows the value that is addresses currently associated with a variable object itself Department of Computer Science University of San Fra Activation Records Simplifying assumptions Code section begins at address 0 Global data at 1000 Runtime stack starts at 2000 Heap starts at 5000 Space for a return value Space for local variables Integers get 4 bytes Chars get 2 bytes Each time a method is called its activation record is pushed onto the stack floats get 8 bytes When the method exits its activation record is removed Department of Computer Science University of San Francisco p 10 Department of Computer Science University of San Francisco p 11 Department of Computer Science University of San Fran 9 13 Activation Records In class trace JavaSimple java On your own try Baseball example Department of Computer Science University of San Francisco p 13
View Full Document
Unlocking...