CS106A, Stanford University

Slides:



Advertisements
Similar presentations
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Advertisements

Recursion CS 367 – Introduction to Data Structures.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Chapter 7 Objects and Memory. Structure of memory The fundamental unit of memory is called a bit, either 0 or 1. In most modern architectures, the smallest.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
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.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
CS107 References and Arrays By Chris Pable Spring 2009.
1 CIS 2168 Data Structures and Algorithms in JAVA Brief Introduction to JAVA.
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.
Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Arrays  an array of 5 ints is filled with 3,2,4,1,7 int a[5] = {3,2,4,1,7}; // note squiggly brackets  an array of 3 floats is filled with 3.2,1.4,0;
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
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.
Function Parameters and Overloading Version 1.0. Topics Call-by-value Call-by-reference Call-by-address Constant parameters Function overloading Default.
CS 106A, Lecture 27 Final Exam Review 1
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
CSE 374 Programming Concepts & Tools
Arrays (review) CSE 2011 Winter May 2018.
Arrays Low level collections.
Review.
CS106A, Stanford University
CS106A, Stanford University
CS 106A, Lecture 15 Events and Memory
Pointers and Dynamic Variables
Advanced Programming Behnam Hatami Fall 2017.
Dynamic Memory Allocation
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
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Object Oriented Programming COP3330 / CGS5409
CS106A, Stanford University
CSC 253 Lecture 8.
Variables and Their scope
SSEA Computer Science: Track A
CS106A, Stanford University
Given the code to the left:
CS106A, Stanford University
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Recap Week 2 and 3.
CS106A, Stanford University
Arrays an array of 5 ints is filled with 3,2,4,1,7
CS106A, Stanford University
Introduction to Primitives
Introduction to Primitives
CS106A, Stanford University
A+ Computer Science PARAMETERS
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

Be able to trace memory with references Learning Goals Be able to trace memory with references

Write this program

Who thinks this prints true?

Who thinks this prints true? public void run() { int x = 5; int y = 5; println(x == y); } true

Who thinks this prints true?

Class of the 10 keys

Advanced memory model

Core memory model

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 public void run() { println(toInches(5)); } private int toInches(int feet){ int result = feet * 12; return result; f run toInches feet 5

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 ** don’t think on the binary level (yet)

#0: variables have fixed size buckets to store values

End aside

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

Primitives vs Classes Primitive Variable Types Class Variable Types int double char boolean GRect GOval Gline Color aka Objects 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

How do you share wikipedia articles? Antelope Canyon Article Key: https://en.wikipedia.org/wiki/Antelope_Canyon

Objects store addresses All of todays class: Objects store addresses (which are like URLs)

What does an object store?

Objects store addresses (which are like URLs)

By Chris Piech

origin Nick Troccoli By Chris Piech

Once upon a time…

…a variable x was born! int x;

…a variable x was born! int x;

x was a primitive variable… int x; It’s so cuuuute! Aww…!

…and its parents loved it very much. int x; We should give it…. value 27!

…and its parents loved it very much. x = 27; We should give it…. value 27! 27

A few years later, the parents decided to have another variable.

…and a variable rect was born! GRect rect;

rect was an object variable… GRect rect; Who’s a cute GRect??? It’s so square!

…and its parents loved it very much. GRect rect; We should make it…. a big, strong GRect!

…and its parents loved it very much. GRect rect = new GRect(0, 0, 50, 50); We should make it…. a big, strong GRect!

…but rect’s box was not big enough for an object! GRect rect = new GRect(0, 0, 50, 50); That box isn’t big enough to store everything about a GRect!

…so they stored the information in a bigger box somewhere else. GRect rect = new GRect(0, 0, 50, 50); x = 0, y = 0 width = 50 height = 50 ... Stack and heap Location 5 See location 5

In practice

public void run() { GRect r = null; } Method memory Object memory

public void run() { GRect r = null; } Method memory Object memory run r null

Wahoo!

public void run() { GRect r = new GRect(50, 50); } Method memory Object memory run r

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

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

public void run() { GRect r = new GRect(50, 50); } Method memory Object memory run 18 r 18

public void run() { GRect r = new GRect(50, 50); } Method memory Object memory 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); } Method memory Object memory 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); } Method memory Object memory 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); } Method memory Object memory run r

#1: new allocates memory for objects * The data for an object can’t always fit inside a fixed size bucket

#2: object variables store addresses #ultimatekey

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

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

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

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

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

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

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

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

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 32 run first second

stack heap 32 run first 32 second

stack heap 32 run first 32 second

stack heap 32 run first 32 second 32

stack heap 32 run first 32 second 32

stack heap 32 run first 32 second 32

stack heap 32 run first 32 second 32

stack heap 32 run first 32 second 32

stack heap 32 run first 32 second 32

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

What does an object store?

Objects store addresses (which are like URLs)

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 18 run paddle 18

stack heap 18 run paddle 18 makeBlue

stack heap 18 run paddle 18 makeBlue object

stack heap 18 run paddle 18 makeBlue object 18

stack heap 18 run paddle 18 makeBlue object 18

stack heap 18 run paddle 18 makeBlue object 18

stack heap 18 run paddle 18 makeBlue object 18

stack heap 18 run paddle 18 makeBlue object 18

stack heap 18 run paddle 18 makeBlue object 18

stack heap 18 run paddle 18 makeBlue object 18

stack heap 18 run paddle 18

stack heap 18 run paddle 18

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

What does an object store?

Objects store addresses (which are like URLs)

heap Instance Variables

heap 18 Instance Variables paddle 18

heap 18 Instance Variables paddle 18 run

heap 18 Instance Variables paddle 18 run

#7: there is space for all instance variables #7: there is space for all instance variables. They are accessible by the entire class

#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.

#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? public void run() { int x = 5; int y = 5; println(x == y); } true

Who thinks this prints true?

What does an object store?

Objects store addresses (which are like URLs)

Milestones Milestone 1 Milestone 2

Finish Up

Be able to trace memory with references Learning Goals Be able to trace memory with references