CS106A, Stanford University

Slides:



Advertisements
Similar presentations
CISC Data Structures Ben Perry University of Delaware Summer 2011.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Introduction to Programming Lecture 39. Copy Constructor.
Analysis of programs with pointers. Simple example What are the dependences in this program? Problem: just looking at variable names will not give you.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Memory Allocation Recall: we use a reference variable to refer to instances of a class. The value in a reference variable is, essentially, a pointer to.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
CS107 References and Arrays By Chris Pable Spring 2009.
Object-Oriented Programming in C++
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Arrays. 2 Till now we are able to declare and initialize few variables Reality: need to compute on a large amount of data Arrays are data structures that.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
3.1 Objects. Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can.
CS 106A, Lecture 27 Final Exam Review 1
Memory Management.
Values vs. References Lecture 13.
CSE 374 Programming Concepts & Tools
Arrays (review) CSE 2011 Winter May 2018.
Review.
CS106A, Stanford University
CS 106A, Lecture 15 Events and Memory
Pointers and Dynamic Variables
CS 106A, Lecture 12 More Graphics
CSC 253 Lecture 8.
CS 106A, Lecture 7 Parameters and Return
CS106A, Stanford University
CS 106A, Lecture 22 More Classes
CS106A, Stanford University
CSC 253 Lecture 8.
SSEA Computer Science: Track A
Dynamic Memory A whole heap of fun….
Using PARAMETERS In Java.
CS106A, Stanford University
Given the code to the left:
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
CS106A, Stanford University
CS106A, Stanford University
Dynamic Memory A whole heap of fun….
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Dynamic Memory A whole heap of fun….
CS106A, Stanford University
Arrays an array of 5 ints is filled with 3,2,4,1,7
CS106A, Stanford University
Dynamic Memory A whole heap of fun….
C Programming Lecture-8 Pointers and Memory Management
Java Programming Language
C Programming Pointers
CS106A, Stanford University
The Stack.
References Revisted (Ch 5)
Pointers, Dynamic Data, and Reference Types
Classes and Objects Object Creation
SPL – PS2 C++ Memory Handling.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

CS106A, Stanford University Memory Chris Piech CS106A, Stanford University

Learning Goals Be able to trace memory for objects Be able to trace memory with instance variables

Who thinks this prints true?

Who thinks this prints true?

Deep Understanding is Key

[suspense]

Today’s Route You are here Friday Instance Variables Canvas The Heap Core model The River of Memories

Core memory model

Stack Diagrams public void run() { println(toInches(5)); } private int toInches(int feet){ int result = feet * 12; return result;

Stack Diagrams public void run() { println(toInches(5)); } private int toInches(int feet){ int result = feet * 12; return result; run

Stack Diagrams public void run() { println(toInches(5)); } private int toInches(int feet){ int result = feet * 12; return result; run

Stack Diagrams 5 run toInches feet result public void run() { println(toInches(5)); } private int toInches(int feet){ int result = feet * 12; return result; f run toInches feet 5 result

Stack Diagrams 5 60 run toInches feet result public void run() { println(toInches(5)); } private int toInches(int feet){ int result = feet * 12; return result; f run toInches feet 5 result 60

Stack Diagrams 5 60 run toInches feet stack result public void run() { println(toInches(5)); } private int toInches(int feet){ int result = feet * 12; return result; f run toInches feet 5 stack result 60

Stack Diagrams public void run() { println(toInches(5)); } private int toInches(int feet){ int result = feet * 12; return result; f run 60

Aside: Actual Memory

What is a bucket feet 5

What is a bucket 1011100111100011 0011010110010100 feet * Each bucket or “word” holds 64 bits

What does memory look like? public void run() { println(toInches(5)); } private int toInches(int feet){ int result = feet * 12; return result;

Stack Diagrams run toInches 5 inches 60 result

Actual Memory 0011010011010001 run overhead 1010110011010111 1111100000111100 0000111100001111 toInches overhead 1011100111100011 0011010110010100 feet 0101101110111101 1111011111101111 result ? ?

#0: don’t think on the binary level (yet)

Primitives vs Classes Primitive Variable Types Class Variable Types int double char boolean GRect GOval Gline Color Class variables (aka objects) Have upper camel case types You can call methods on them Are constructed using new Are stored in a special way

Objects GRect myRect = new GRect(20, 20); Object

The Heap

public void run() { GRect r = null; } stack

public void run() { GRect r = null; } stack run r null

Wahoo!

public void run() { GRect r = new GRect(50, 50); } stack heap run r

public void run() { GRect r = new GRect(50, 50); } stack heap run 18 r

public void run() { GRect r = new GRect(50, 50); } stack heap run 18 r 18

public void run() { GRect r = new GRect(50, 50); } stack heap memory.com/18 run r memory.com/18

public void run() { GRect r = new GRect(50, 50); } stack heap run r

public void run() { GRect r = new GRect(50, 50); r. setColor(Color public void run() { GRect r = new GRect(50, 50); r.setColor(Color.BLUE); r.setFilled(true); } stack heap run r

public void run() { GRect r = new GRect(50, 50); r. setColor(Color public void run() { GRect r = new GRect(50, 50); r.setColor(Color.BLUE); r.setFilled(true); } stack heap run r

public void run() { GRect r = new GRect(50, 50); r. setColor(Color public void run() { GRect r = new GRect(50, 50); r.setColor(Color.BLUE); r.setFilled(true); } stack heap run r

What does an object store?

An object stores a memory address!

#1: new allocates memory on the heap

#2: object variables live on the stack and store heap addresses

public void run() { GImage img = new GImage(”mountain public void run() { GImage img = new GImage(”mountain.jpg”); add(img, 0, 0); } stack heap run img

#3: GImages look impressive but don’t take much extra work

stack heap run first second

stack heap run first second

stack heap run first second

stack heap 32 run first 32 second

stack heap 32 run first 32 second 32

stack heap run first second

stack heap run first second

#4: when you use the = operator with objects, it copies the address

Passing by “Reference”

Primitives pass by value // NOTE: This program is buggy!! public void run() { int x = 3; addFive(x); println("x = " + x); } private void addFive(int x) { x += 5; * This is probably the single more important example to understand in CS106A

Objects pass by reference // NOTE: This program is awesome!! public void run() { GRect paddle = new GRect(50, 50); makeBlue(paddle); add(paddle, 0, 0); } private void makeBlue(GRect object) { object.setColor(Color.BLUE); object.setFilled(true); * This is probably the single more important example to understand in CS106A

stack heap run paddle

stack heap run paddle

stack heap 18 run paddle 18 makeBlue object

stack heap 18 run paddle 18 makeBlue object 18

stack heap run paddle makeBlue object

stack heap run paddle makeBlue object

stack heap run paddle makeBlue object

stack heap run paddle

stack heap run paddle

#5: when you pass (or return) an object, the address is passed. Aka reference

What does an object store?

An object stores a memory address!

public void run() { GRect r = new GRect(50, 50); } stack heap run 18 r 18

Canvas

Canvas Heap Instance Variables 12 canvas 12 run 12 r

Canvas Heap Instance Variables 12 canvas 94 mousePressed x = 72 94 e y = 94 time = 192332123 e 12 obj

Canvas Heap Instance Variables 12 canvas 94 mousePressed x = 72 94 e y = 94 time = 192332123 e 12 obj

#6: graphics programs all have a “canvas” which keeps track of the objects on the screen

Intentionally left blank so that we can fill it in during lecture

What does an object store?

An object stores a memory address!

Instance Variables

heap Instance Variables canvas paddle

heap Instance Variables canvas paddle

heap Instance Variables canvas paddle run

heap Instance Variables canvas paddle run

heap Instance Variables canvas paddle run

#7: there is space for all instance variables #7: there is space for all instance variables. They are globally accessible

#8: instance variables are initialized before run is called

Question: what does this program do? Common Bug Question: what does this program do? Answer: makes a square that is 0 by 0 since getWidth is called before the screen has been made.

Today’s Route You are here Friday Instance Variables Canvas The Heap Core model The River of Memories

#9: for objects, == checks if the variables store the same address

Recall the start of class?

Who thinks this prints true?

Who thinks this prints true?

What does an object store?

An object stores a memory address!

Learning Goals Be able to write a large program Be able to trace memory with references

Beyond CS106A

Beyond CS106A Heap run 12 76 this r 12 Instance Variables 76 canvas 12 76 Technically, instance variables are stored on the heap and each method has access to a special (hidden) variable called a “this” pointer.