C11: Garbage Collection and Constructors

Similar documents
C10: Garbage Collection and Constructors

C18a: Abstract Class and Method

C09: Interface and Abstract Class and Method

Contents. 8-1 Copyright (c) N. Afshartous

C08: Inheritance and Polymorphism

Dynamic Allocation of Memory

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

C09: Interface, and Abstract Class and Method

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

CMSC 330: Organization of Programming Languages

Chapter 2. Procedural Programming

Class Information ANNOUCEMENTS

CMSC 132: Object-Oriented Programming II

In Java we have the keyword null, which is the value of an uninitialized reference type

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Name, Scope, and Binding. Outline [1]

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Java Magistère BFA

C12a: The Object Superclass and Selected Methods

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Short Notes of CS201

CPSC 427: Object-Oriented Programming

Class, Variable, Constructor, Object, Method Questions

C02: Overview of Software Development and Java

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

CS201 - Introduction to Programming Glossary By

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

C08: Inheritance and Polymorphism

Memory and C++ Pointers

Object-Oriented Programming for Scientific Computing

CPSC 3740 Programming Languages University of Lethbridge. Data Types

CS201- Introduction to Programming Current Quizzes

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

COMP 2355 Introduction to Systems Programming

Instance Members and Static Members

CSE 100: C++ TEMPLATES AND ITERATORS

CSC1322 Object-Oriented Programming Concepts

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Memory management COSC346

Scope and Parameter Passing

Vector and Free Store (Vectors and Arrays)

Java Fundamentals (II)

Lecture 23: Object Lifetime and Garbage Collection

Classes and Inheritance Extending Classes, Chapter 5.2

CMSC 132: Object-Oriented Programming II

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

COMP6771 Advanced C++ Programming

CA31-1K DIS. Pointers. TA: You Lu

Pointers and Dynamic Memory Allocation

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Lecture 2, September 4

Pointers and Terminal Control

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

CSE 303: Concepts and Tools for Software Development

Lecture 15a Persistent Memory & Shared Pointers

Structure of Programming Languages Lecture 10

Introduction to Programming Using Java (98-388)

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Week 5-1: ADT Design

Scope and Parameter Passing

CISC 3115 TY3. C21a: Recursion. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/6/2018 CUNY Brooklyn College

Dynamic Allocation of Memory

Objects Managing a Resource

CPSC 427: Object-Oriented Programming

CSE 307: Principles of Programming Languages

Object-Oriented Programming More Inheritance

Scope. Scope is such an important thing that we ll review what we know about scope now:

Chapter 6 Introduction to Defining Classes

Assignment of Structs

G Programming Languages - Fall 2012

Pointers II. Class 31

内存管理. Memory management

CMSC 341 Lecture 2 Dynamic Memory and Pointers

C++ Programming Lecture 4 Software Engineering Group

Run-time Environments

10. Abstract Data Types

Run-time Environments

Outline. Dynamic Memory Classes Dynamic Memory Errors In-class Work. 1 Chapter 10: C++ Dynamic Memory

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

Exercise: Singleton 1

Example: Count of Points

Introduction to C++ Part II. Søren Debois. Department of Theoretical Computer Science IT University of Copenhagen. September 12th, 2005

Vector and Free Store (Pointers and Memory Allocation)

Lecture 6 Introduction to Objects and Classes

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

Object Oriented Modeling

Spring 2019 Discussion 4: February 11, 2019

Memory Leak. C++: Memory Problems. Memory Leak. Memory Leak. Pointer Ownership. Memory Leak

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

CSI33 Data Structures

Inheritance. Transitivity

Transcription:

CISC 3120 C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017 CUNY Brooklyn College 1

Outline Recap Project progress and lessons learned Memory management Java garbage collection Constructors Assignments 10/5/2017 CUNY Brooklyn College 2

Memory Management in C++ In C++: compare the following two. Cat *ginger = new Cat(); ginger = nullptr; Cat *ginger = new Cat(); delete ginger; ginger = nullptr; 10/5/2017 CUNY Brooklyn College 3

Memory Management in C++ In C++: compare the following two. Cat *ginger = new Cat(); ginger = nullptr; Cat *ginger = new Cat(); delete ginger; ginger = nullptr; A programmer must explicitly free the memory allocated to an object in a C++ program; otherwise, the memory cannot be reclaimed and used in the program. 10/5/2017 CUNY Brooklyn College 4

Memory Management in Java In Java: Cat ginger = new Cat(); ginger = null; Should a programmer worry about reclaiming the memory? Java Garbage Collector takes care of it. 10/5/2017 CUNY Brooklyn College 5

Program Data We restrict the definition of program data to data associated with variables In C++ and Java, program data are in three categories Automatic Static Dynamic 10/5/2017 CUNY Brooklyn College 6

Automatic, Static, and Dynamic Program Data They differ in which region of memory the data reside when and how the data is allocated in memory when and how the data is deallocated in memory Variables where: scope when: lifetime 10/5/2017 CUNY Brooklyn College 7

Automatic Data Memory is automatically allocated and deallocated for automatic data Where: the memory is allocated in a region of memory called stack When to allocate: the memory is allocated when execution reaches the scope of the variable When to deallocate: the memory is deallocated when execution leaves the scope of the variable 10/5/2017 CUNY Brooklyn College 8

Automatic Data: Examples In C++ and Java: where are the variables for automatic data? int sumtonumber(int number) { int sum = 0; for (int i=0; i<number; i++) { sum += i; return sum; 10/5/2017 CUNY Brooklyn College 9

Automatic Data: Examples In C++ and Java: where are the variables for automatic data? Parameter: number Local variables: sum i What are their scopes? int sumtonumber(int number) { int sum = 0; for (int i=0; i<number; i++) { sum += i; return sum; 10/5/2017 CUNY Brooklyn College 10

Automatic Data: Examples In C++ and Java: where are the variables for automatic data? What are their scopes? int sumtonumber(int number) { int sum = 0; i for (int i=0; i<number; i++) { sum += i; sum number return sum; 10/5/2017 CUNY Brooklyn College 11

Static Data Static data s existence does not change during the entire execution of a program Where: the memory for static data is allocated in a region of memory, generically referred to as the static data segment When to allocate: the memory is allocated when the program starts, or when execution reaches the static variable declaration for the first time When to deallocate: the memory for static data is deallocated when the program exits 10/5/2017 CUNY Brooklyn College 12

Static Data: Example in C++ In C++: where are the variables for static data? int sumtonumber(int number) { static int sum = 0; for (int i=0; i<number; i++) { sum += i; return sum; 10/5/2017 CUNY Brooklyn College 13

Static Data: Example in C++ In C++: where are the variables for static data? Local variable: sum What is its scope? int sumtonumber(int number) { static int sum = 0; for (int i=0; i<number; i++) { sum += i; return sum; 10/5/2017 CUNY Brooklyn College 14

Static Data: Example in C++ In C++: where are the variables for static data? What are their scopes? int sumtonumber(int number) { static int sum = 0; for (int i=0; i<number; i++) { sum += i; sum 10/5/2017 CUNY Brooklyn College 15

Static and Automatic Data: Example in C++ In C++: when are they allocated and deallocated? int sumtonumber(int number) { static int sum = 0; for (int i=0; i<number; i++) { sum += i; return sum; int sumtonumber(int number) { int sum = 0; for (int i=0; i<number; i++) { sum += i; return sum; 10/5/2017 CUNY Brooklyn College 16

Static and Automatic Data: Example in C++ How do the lifetimes of the automatic and static variables differ? Compare them in running programs When sum is static When sum is automatic cout << sumtonumber(5) << endl; cout << sumtonumber(5) << endl; 10/5/2017 CUNY Brooklyn College 17

Static Data in C++ In C++ Variables declared as static Additionally, variables declared outside any function and class body 10/5/2017 CUNY Brooklyn College 18

Static Data: Example in Java Can we write the following in Java? int sumtonumber(int number) { static int sum = 0; for (int i=0; i<number; i++) { sum += i; return sum; 10/5/2017 CUNY Brooklyn College 19

Static Data in Java Can we write the following in Java? int sumtonumber(int number) { static int sum = 0; for (int i=0; i<number; i++) { sum += i; return sum; 10/5/2017 CUNY Brooklyn College 20

Static Data in Java Java is more restrictive Static variables can only declared within a class, but not within any methods Static variables are class variables with the scope of the class 10/5/2017 CUNY Brooklyn College 21

Static Data: Example in Java Where are the static variables? class StaticSum { static int sum = 0; int sumtonumber(int number) { for (int i=0; i<number; i++) { sum += i; return sum; 10/5/2017 CUNY Brooklyn College 22

Static Data: Example in Java What is its scope? class StaticSum { static int sum = 0; int sumtonumber(int number) { for (int i=0; i<number; i++) { sum += i; return sum; 10/5/2017 CUNY Brooklyn College 23

Static Data: Example in Java What is the output of this program? class StaticSum { public static void main(string[] args) { StaticSum s = new StaticSum(); System.out.println(s.sumToNumber(5)); System.out.println(s.sumToNumber(5)); int sumtonumber(int number) { for (int i=0; i<number; i++) { sum += i; return sum; static int sum = 0; 10/5/2017 CUNY Brooklyn College 24

Static Data in Java Java is more restrictive Static variables can only declared within a class, but not within any methods Static variables are class variables with the scope of the class Static variables has globe scope Static variables has the lifetime of the program 10/5/2017 CUNY Brooklyn College 25

Questions? Start discussing memory management Automatic and static program data 10/5/2017 CUNY Brooklyn College 26

Dynamic Data Programmers are responsible for allocating dynamic data In C++: programmers are also responsible for deallocating the dynamic data. Where: the memory for static data is allocated in a region of memory, generically referred to as the heap When to allocate: the memory is allocated when the programmer invokes the new operator. When to deallocate: In Java: when the Java Garbage Collector reclaims the object allocated In C++: when the programmer invokes the delete operator to free the memory allocated to the dynamic data 10/5/2017 CUNY Brooklyn College 27

Dynamic Data in C++ and Java In C++, programmers can allocate memory for any data types, i.e., to use new operator against any data types In Java, programmers can only use new operator for reference data types 10/5/2017 CUNY Brooklyn College 28

Dynamic Data in C++ and Java: Examples In Java: which one of the following is legal? int i = new int; Cat ginger = new Cat(); int[] iarr = new int[10]; In C++: which one of the following is legal? int *iptr = new int; Cat *gingerptr = new Cat(); int* iarr = new int[10]; 10/5/2017 CUNY Brooklyn College 29

Dynamic Data in C++ and Java: Examples In Java: which one of the following is legal? int i = new int; Cat ginger = new Cat(); int[] iarr = new int[10]; In C++: which one of the following is legal? int *iptr = new int; Cat *gingerptr = new Cat(); int* iarr = new int[10]; 10/5/2017 CUNY Brooklyn College 30

Dynamic Data: Java and C++ Comparison Allocation: The same Java and C++: dynamic data are created using the new operator The different Deallocation In Java: dynamic data can only be objects, cannot be primitive data types. In C++: dynamic data can be any data types The different In C++: programmers use the delete operator to deallocate memory In Java: Java Garbage Collector is responsible for deallocating the memory, programmers have little control. 10/5/2017 CUNY Brooklyn College 31

Objects in Java and C++ C++ can have both automatically and dynamically allocated objects Cat *ginger = new Cat(); Cat ginger; ginger->pounce(); (*ginger).pounce(); ginger.pounce(); Java has only dynamically allocated objects Cat ginger; ginger.pounce(); Cat ginger = new Cat() ginger.pounce(); 10/5/2017 CUNY Brooklyn College 32

Objects in Java and C++ C++ can have both automatically and dynamically allocated objects Cat ginger; ginger.pounce(); Cat *ginger = new Cat(); ginger->pounce(); (*ginger).pounce(); Java has only dynamically allocated objects Cat ginger; ginger.pounce(); Cat ginger = new Cat() ginger.pounce(); 10/5/2017 CUNY Brooklyn College 33

Dynamic Data: Programming Error in C++ Programmers are responsible for managing dynamic data, which is error-prone Common errors Inaccessible objects Memory leaks Dangling pointers 10/5/2017 CUNY Brooklyn College 34

Java Garbage Collector Java is responsible for deallocating dynamic data, and programmers are not. In Java, we often write Cat ginger = new Cat(); ginger.pounce(new Animal()); In C++, we never write (although it compiles) Cat *ginger = new Cat(); ginger->pounce(new Animal()); 10/5/2017 CUNY Brooklyn College 35

Different Garbage Collection Algorithms How does a Garbage Collector figure out an object is no longer needed and can be deallocated? Reference counting Trace-based garbage collector e.g., Baker s algorithm Copying collector 10/5/2017 CUNY Brooklyn College 36

Advantage of Garbage Collection Avoid bugs, such as, Forget to free memory (memory leak) Use already freed objects (dangling pointers) Also in Java, programmers do not have direct memory access, and cannot accidentally overwrite memory. 10/5/2017 CUNY Brooklyn College 37

Disadvantage of Garbage Collection Consume resources (memory and processor) Unpredictable stalls Memory leak still possible, but harder to understand No manual control 10/5/2017 CUNY Brooklyn College 38

Questions Concept of Garbage Collector Programming in Java that does garbage collection 10/5/2017 CUNY Brooklyn College 39

Constructors Like C++, constructors in Java have the identical name as the name of the class, do not specify return type, are called when an object is created, and are responsible for initializing the object (instance variables) 10/5/2017 CUNY Brooklyn College 40

Default Constructor Java compiler provides the default constructor when no constructor is written. class Cat { void pounce(cat othercat) { Cat ginger = new Cat(); // calling default constructor ginger.pounce(new Cat()); 10/5/2017 CUNY Brooklyn College 41

Default Constructor? class Cat { private String name; public Cat(String name) {this.name = name; void pounce(cat othercat) { Can we use the default constructor now? Cat ginger = new Cat(); ginger.pounce(new Cat( tiger )); 10/5/2017 CUNY Brooklyn College 42

Default Constructor? class Cat { private String name; public Cat(String name) {this.name = name; void pounce(cat othercat) { Can we use the default constructor now? Cat ginger = new Cat(); ginger.pounce(new Cat( tiger )); 10/5/2017 CUNY Brooklyn College 43

Default and Parameterized Constructors Java ceases to create the default constructor class Cat { private String name; public Cat() {name = cat ; public Cat(String name) {this.name = name; void pounce(cat othercat) { Cat ginger = new Cat(); 10/5/2017 CUNY Brooklyn College 44

Constructor and Inheritance When we create an object of a class, constructors of all superclasses must be called explicitly or implicitly Can you name the constructors being called for this example? Animal Feline Panther brave = new Panther( brave ); Cat Panther 10/5/2017 CUNY Brooklyn College 45

Calling Super Class s Constructor Implicitly What if we write the constructor as follows, class Panther extends Feline { Color color; public Panther(String name, Color color) { this.color = color; public void makenoise() { Cat Animal Feline Panther 10/5/2017 CUNY Brooklyn College 46

Calling Super Class s Constructor Implicitly Java compiler will call Feline s default constructor. Animal class Panther extends Feline { Color color; public Panther(String name, Color color) { Feline this.color = color; public void makenoise() { Cat Panther 10/5/2017 CUNY Brooklyn College 47

Calling Super Class s Constructor Explicitly Use super class Panther extends Feline { Animal Color color; public Panther(String name, Color color) { super(name); this.color = color; Feline public void makenoise() { Cat Panther 10/5/2017 CUNY Brooklyn College 48

Recap: Stack and Heap Two important region of memories Stack Heap 10/5/2017 CUNY Brooklyn College 49

Stack Methods are stacked Stack is organized as stack frames A stack frame holds the state of the method (method invocation and automatic data) Program counter: which line of code being executed Automatic data: values of method parameters and local variables 10/5/2017 CUNY Brooklyn College 50

Stack: Example dostuff gets called dostuff() b go(4) dostuff() go(4) x, z dostuff() b crazy() c go(4) x, z dostuff() b x, z b void dostuff() { boolean b = true; go(4); void go(int x) { int z = x + 24; crazy(); Void crazy() { int c = 36; 10/5/2017 CUNY Brooklyn College 51

Heap Objects including their instance variables live Cat ginger = new Cat() ; Panther brave = new Panther(); The ginger object The brave object ginger Stack brave Heap 10/5/2017 CUNY Brooklyn College 52

Questions? Constructors Default constructor Overloading constructors Inheritance and constructors this and super Stack and heap 10/5/2017 CUNY Brooklyn College 53

Assignments Project 2 How is it going? Practice assignments To be available via CUNY Blackboard 10/5/2017 CUNY Brooklyn College 54