Memory Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Similar documents
CS 106A, Lecture 7 Parameters and Return

Nested Loops Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

CS106A Review Session

CS 106A, Lecture 27 Final Exam Review 1

Primitives, Objects, and Heap/Stack Review 1 Chris Piech CS106A, Stanford University

CS 106A, Lecture 27 Final Exam Review 1

CS 106A, Lecture 11 Graphics

CS 106A, Lecture 14 Events and Instance Variables

CS 106A, Lecture 14 Events and Instance Variables

CS 106A, Lecture 25 Life After CS 106A, Part 1

Drawing Geometrical Objects. Graphic courtesy of Eric Roberts

CS 106A, Lecture 23 Interactors and GCanvas

CS 106A, Lecture 5 Booleans and Control Flow

CS 106A, Lecture 16 Arrays

CS 106A, Lecture 24 Interactors and NameSurfer

CS 106X, Lecture 16 More Linked Lists

CS 106A, Lecture 3 Problem-solving with Karel

CS 106A, Lecture 9 Problem-Solving with Strings

CS 106X, Lecture 10 Recursive Backtracking

CS 106X Lecture 26: Inheritance and Polymorphism in C++

CS 106A, Lecture 3 Problem-solving with Karel

Expressions, Statements, and Control Structures

CS 106A, Lecture 19 ArrayLists

CS 106X, Lecture 14 Classes and Pointers

CS 106B, Lecture 1 Introduction to C++

CS 106A, Lecture 9 Problem-Solving with Strings

CS106A Final Exam Review Session. Saturday Dec. 10, 2016 Nick Troccoli

CS 106A, Lecture 20 ArrayLists and HashMaps

Data Structure Design II Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Solutions for Section #4

Queues and Unit Testing

Objects and Graphics

CS106A Review Session. Monday Oct. 31, 2016 Nick Troccoli

CS 106X, Lecture 23 Dijkstra and A* Search

Hangman YEAH Hours. Thursday, February 14, 7:30 9:00PM Andrew Tierno

Common Misunderstandings from Exam 1 Material

CS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017

Events Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

October 27, 7:30-9:30 PM Taylor Bacon and Kashif Nazir (Based on slides by Nick Troccoli)

Solutions for Section #2

CS 106X, Lecture 7 Introduction to Recursion

Animation. Piech, CS106A, Stanford University

Arrays Chris Piech CS106A, Stanford University

Section Handout #6: HashMaps, ArrayLists, and Classes

Simple Java YEAH Hours. Brahm Capoor and Vrinda Vasavada

Control Structures and Methods

CS 101 Computer Programming

Solutions for Section #2

Assignment #3 Hangman Due: 11AM PST on Thursday, July 20th

Classes. How can we manage large programs?

Section 05: Midterm Review

Homework Assignment 2: Java Console and Graphics

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

What goes inside when you declare a variable?

Solution to Section #3 Portions of this handout by Eric Roberts, Mehran Sahami, Marty Stepp, Patrick Young and Jeremy Keeshin

Using Eclipse and Karel

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

CS107, Lecture 9 C Generics Function Pointers

Control Statements. if for while

Assumptions. History

Using Machine Learning to Model How Students Learn to Program

Variables, Types, and Expressions

Assignment #1: /Survey and Karel the Robot Karel problems due: 1:30pm on Friday, October 7th

Section Handout #3: Strings and Files

Multimedia-Programmierung Übung 3

Topic 4 Expressions and variables

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

CS107, Lecture 6 More Pointers and Arrays

What is Java? professional software engineering.

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Variables Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Spring 2018 June 20 th, 2018 CS106A Practice Final #1

Programming Abstractions

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Assignment 6 YEAH Hours. Ben Barnett and Avery Wang

Unit 5: More on Classes/Objects Notes

Winter 2017 Feb 13 th, 2017 CS106A Midterm. Last Name: First Name: Sunet ID (eg jdoe): Section Leader / Grader:

Practice Midterm Examination

Solutions to Section #7

CMSC 113: Computer Science I Review Questions for Exam #1

CS 106X, Lecture 2 C++ Functions and Strings

Programming Abstractions

CMSC131. Library Classes

Announcements. Lecture 04b Header Classes. Review (again) Comments on PA1 & PA2. Warning about Arrays. Arrays 9/15/17

CS 170 Java Programming 1. Week 7: More on Logic

Chris Piech Handout #21 CS 106A March 7, 2018

Assignment 6: Huffman Encoding

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Solution to Section #5

Spring 2018 Mentoring 8: March 14, Binary Trees

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

Memory and C++ Pointers

Practice Final Examination #2

Friday Four Square! Today at 4:15PM, Outside Gates

Unit testing. JUnit. JUnit and Eclipse. A JUnit test class 3/6/17. A method is flagged as a JUnit test case.

CS 106A Winter 2017 Final Solutions

Recursion. Garfield AP Computer Science. As usual, significant borrowings from Stuart Reges and Marty Stepp at UW -- thanks!!

HEAPS & PRIORITY QUEUES

Transcription:

Memory Chris Piech CS106A, Stanford University

Learning Goals 1. 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); }

Who thinks this prints true?

Class of the 10 keys

Advanced memory model

Core memory model

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

Stack Diagrams public void run() { println(toinches(5)); } run 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; } f run toinches feet 5

Stack Diagrams 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 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)); } run private int toinches(int feet){ int result = feet * 12; return result; } f 60

Aside: Actual Memory

What is a bucket feet 5

What is a bucket feet 1011100111100011 0011010110010100 * 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 int double char boolean Class Variable Types GRect GOval Gline Color Class variables (aka objects) 1. Have upper camel case types 2. You can call methods on them 3. Are constructed using new 4. 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) 1. Have upper camel case types 2. You can call methods on them 3. Are constructed using new 4. Are stored in a special way

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

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 27

origin Nick Troccoli By Chris Piech 28

Once upon a time This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, and others.

a variable x was born! int x; 30

a variable x was born! int x; 31

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

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

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

A few years later, the parents decided to have another variable. This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, and others.

and a variable rect was born! GRect rect; 36

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

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

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

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! 40

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... See location 5 Location 5 41

In practice This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, and others.

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 run Object memory memory.com/18 r

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

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

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.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.jpg ); add(img, 0, 0); } stack heap run

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

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

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

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

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

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

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

stack heap run 32 first 32 second

stack heap run 32 first 32 second

stack heap run 32 first 32 second 32

stack heap run 32 first 32 second 32

stack heap run 32 first 32 second 32

stack heap run 32 first 32 second 32

stack heap run 32 first 32 second 32

stack heap run 32 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 run 18 paddle 18

stack heap run 18 paddle 18 makeblue

stack heap run 18 paddle 18 makeblue object

stack heap run 18 paddle 18 makeblue object 18

stack heap run 18 paddle 18 makeblue object 18

stack heap run 18 paddle 18 makeblue object 18

stack heap run 18 paddle 18 makeblue object 18

stack heap run 18 paddle 18 makeblue object 18

stack heap run 18 paddle 18 makeblue object 18

stack heap run 18 paddle 18 makeblue object 18

stack heap run 18 paddle 18

stack heap run 18 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)

Instance Variables heap

Instance Variables 18 heap paddle 18

Instance Variables 18 heap paddle 18 run

Instance Variables 18 heap paddle 18 run

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

#8: instance variables are initialized before run is called

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); }

Who thinks this prints true?

What does an object store?

Objects store addresses (which are like URLs)

Milestones Milestone 1 Milestone 2

Finish Up

Learning Goals 1. Be able to trace memory with references