CSC212 Data Structure - Section RS

Slides:



Advertisements
Similar presentations
@ Zhigang Zhu, CSC212 Data Structure - Section RS Lectures 6/7 Pointers and Dynamic Arrays Instructor: Zhigang Zhu Department of Computer Science.
Advertisements

Reviews for Exam 1 Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, Fall 2010.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Zhigang Zhu Department.
1 CSC212 Data Structure - Section FG Lectures 4 & 5 Container Classes Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Reviews for Exam 1 Chapter 1-4 CS 211 Data Structures MHC, 2007.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
Pointers1 Pointers & Dynamic Arrays Allocating memory at run-time.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
OOP Languages: Java vs C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Xiaoyan Li, CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer.
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
Edgardo Molina, CSC212 Data Structure - Section AB Lectures 6/7 Pointers and Dynamic Arrays Instructor: Edgardo Molina Department of Computer Science.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Edgardo Molina, CSC212 Data Structure - Section AB Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Edgardo Molina Department.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Xiaoyan Li, CSC211 Data Structures Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Prof. Xiaoyan Li Department of Computer Science.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
@ George Wolberg, CSC212 Data Structure Lecture 6 Dynamic Classes and the Law of the Big Three Instructor: George Wolberg Department of Computer.
On dynamic memory and classes ● We previously had only discussed dynamic memory in regards to structs and dynamic arrays. ● However, they can be used (to.
Memory Management.
Constructors and Destructors
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
Pointers, Polymorphism, and Memory Allocation
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
Constructor & Destructor
Class: Special Topics Copy Constructors Static members Friends this
Chapter 1-4 CSc 212 Data Structures, Sec AB CCNY, Spring 2012
This pointer, Dynamic memory allocation, Constructors and Destructor
The Bag and Sequence Classes with Linked Lists
Dynamically Allocated Memory
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Array Lists Chapter 6 Section 6.1 to 6.3
Classes with Dynamically Allocated Data
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
CSC212 Data Structure - Section FG
Constructors and Other Tools
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
CISC/CMPE320 - Prof. McLeod
Objects Managing a Resource
9-10 Classes: A Deeper Look.
Data Structures and Algorithms Memory allocation and Dynamic Array
Class: Special Topics 2 For classes using memory allocation
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Rule of Three Part 1 & 2.
CS 144 Advanced C++ Programming April 30 Class Meeting
9-10 Classes: A Deeper Look.
Copy Constructors and Overloaded Assignment
Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, 2009
SPL – PS3 C++ Classes.
Presentation transcript:

CSC212 Data Structure - Section RS Lecture 8 Dynamic Classes and the Law of the Big Three Instructor: Zhigang Zhu Department of Computer Science City College of New York

Why Dynamic Classes Limitation of our bag class Solution: bag::CAPACITY constant determines the capacity of every bag wasteful and hard to reuse Solution: provide control over size in running time, by pointers and dynamic memory => dynamic arrays => dynamic classes wasteful if we give a big capacity and need to change source code and re-compile for different sizes

Dynamic Classes New Features (Ch 4.3–4) Pointers Member Variables Dynamic Memory Allocation (where and how) Value Semantics (what’s new?) assignment operator overloading your own copy constructor Introducing Destructor Conclusion: the Law of the Big Three Several new factors using dynamic memory

Pointer Member Variable The Static bag The Dynamic bag // From bag1.h in Section 3.1 class bag { public: static const size_t CAPACITY = 20; ... private: value_type data[CAPACITY]; size_type used; }; // From bag2.h in Section 4.3 class bag { public: ... private: value_type *data; size_type used; size_type capacity; }; What are the differences? data[CAPACITY) -> *data capacity is now a variable Why not *data_ptr? What’s next? – dynamic memory allocation

Invariant of the Dynamic bag the number of items is in the member variable used The actual items are stored in a partially filled array. The array is a dynamic array, pointed to by the pointer variable data The total size of the dynamic array is the member variable capacity Recall what is the invariant of a class (p. 105) The rules that dictate how the member variables of a class represent a value implicit part of pre- and post- so is not usually written as an explicit part of pre- and post- about the private member variables, thus for implementation of functions, but not for use of them documented in the implementation file What’s next? Invariant is about rules of implementation...

Allocate Dynamic Memory: Where? In Old Member Functions constructor – how big is the initial capacity? insert – if bag is full, how many more? +/+= operators – how to combine two bags? New Member Functions reserve – explicitly adjust the capacity Example constructor with default size

Allocate Dynamic Memory: How? In constructor: why initialize? how? default specific size // From bag2.h in Section 4.3 class bag { public: static const size_t DEFAULT_CAPACITY = 20; bag(size_type init_cap = DEFAULT_CAPACITY); ... private: value_type *data; size_type used; size_type capacity; }; // From implementation file bag2.cxx bag::bag(size_type init_cap) { data = new value_type[init_cap]; capacity = init_cap; used = 0; }

Value Semantics Assignment operator Copy constructor y = x; Copy constructor bag y(x); // bag y = x; Automatic assignment operator and copy constructor copy all the member variables (data, used, capacity) from object x to object y but our days of easy contentment are done!

Failure in auto assignment operator capacity used data bag x(4), y(5); x.insert(18); x.insert(19); y=x; x.insert(20); x 4 984 ? [0] [1] [2] [3] y ? 5 964 Draw memory map on blackboard 500 x 512 y 964 y.data 984 x.data 1000 bottom [0] [1] [2] [3] [4] Question: What will happen after executing lines 2 – 5?

Failure in auto assignment operator capacity used data bag x(4), y(5); x.insert(18); x.insert(19); y=x; x.insert(20); x 4 2 984 18 19 ? [0] [1] [2] [3] y ? 5 964 Draw memory map on blackboard 500 x 512 y 964 y.data 984 x.data 1000 bottom [0] [1] [2] [3] [4] Question: What will happen after executing lines 2 – 5?

Failure in auto assignment operator capacity used data bag x(4), y(5); x.insert(18); x.insert(19); y=x; x.insert(20); x 4 2 984 18 19 ? [0] [1] [2] [3] y ? 4 2 984 Draw memory map on blackboard 500 x 512 y 964 y.data 984 x.data 1000 bottom [0] [1] [2] [3] [4] lost memory Question: What will happen after executing lines 2 – 5?

Failure in auto assignment operator capacity used data bag x(4), y(5); x.insert(18); x.insert(19); y=x; x.insert(20); x 4 3 984 18 19 20 ? [0] [1] [2] [3] y ? 4 2 984 Draw memory map on blackboard 500 x 512 y 964 y.data 984 x.data 1000 bottom [0] [1] [2] [3] [4] lost memory Consequence: Change to x’ array will also change y’s array

If we want y to have its own dynamic array capacity used data bag x(4), y(5); x.insert(18); x.insert(19); y=x; x.insert(20); x 4 2 984 18 19 ? [0] [1] [2] [3] y ? 5 964 Draw memory map on blackboard 500 x 512 y 964 y.data 984 x.data 1000 bottom [0] [1] [2] [3] [4]

Dynamic memory allocation is needed capacity used data bag x(4), y(5); x.insert(18); x.insert(19); y=x; x.insert(20); x 4 2 984 18 19 ? [0] [1] [2] [3] y 4 2 964 18 19 ? Draw memory map on blackboard 500 x 512 y 964 y.data 984 x.data 1000 bottom ? [0] [1] [2] [3] [4] memory de-allocated Answer: overloading the assignment operator =

Dynamic memory allocation is needed capacity used data bag x(4), y(5); x.insert(18); x.insert(19); y=x; x.insert(20); x 4 2 984 18 19 20 ? [0] [1] [2] [3] y 4 2 964 18 19 ? Draw memory map on blackboard 500 x 512 y 964 y.data 984 x.data 1000 bottom ? [0] [1] [2] [3] [4] memory de-allocated Answer: overloading the assignment operator =

Solution: overloading assignment operator // From bag2.h in Section 4.3 class bag { public: static const size_t DEFAULT_CAPACITY = 20; bag(size_type init_cap = DEFAULT_CAPACITY); ... private: value_type *data; size_type used; size_type capacity; }; Solution: overloading assignment operator // From implementation file bag2.cxx bag::bag(size_type init_cap) { data = new value_type[init_cap]; capacity = init_cap; used = 0; } Your own assignment operator C++ Requires the overloaded assignment operator to be a member function bag x, y; // OR bag x(4), y(5); // OR.... y=x; // y.operator=(x); Things you shall have known: Before assignment, y as well as x has been declared, meaning that constructor has been called upon each declaration For assignment. you need to copy all the items in x.data to y.data the size of y and x may not be the same before assignment void bag::operator=(const bag& source) // Postcondition: The bag that activated this function has the same items and capacity as source A 5-minute Quiz: write your own implementation - turn in

Implementation of operator= void bag::operator =(const bag& source) // Library facilities used: algorithm { value_type *new_data; // Check for possible self-assignment: if (this == &source) return; // If needed, allocate an array with a different size: if (capacity != source.capacity) new_data = new value_type[source.capacity]; delete [ ] data; // make sure all valid before delete!!! data = new_data; capacity = source.capacity; } // Copy the data from the source array: used = source.used; copy(source.data, source.data + used, data); y = x; y  *this x  source Why put delete after new? Otherwise if new fails, the exception handler will activate the destructor for any object that was constructed previously. With a invalid bag (this), the destructor may cause another error that is more confusing...Who knows what will the printout message??!!

The 2nd part of the value semantics copy constructor

Break Programming Assignment 2 Due Thursday February 25th, 2010 ! Assignment 3 is online , due March 11 (Thu) Next Class: Exam review March 04 Thursday: First Exam 4:00 – 5:40 pm

The 2nd part of the value semantics copy constructor

Auto Copy Constructor: shallow copy capacity used data bag x(4) bag y(x); x.insert(18); x.insert(19); x 4 984 ? [0] [1] [2] [3] y 4 984 If necessary, do this again on blackboard The only difference with auto assignment is: y does not have its own data

Failure in auto copy constructor capacity used data bag x(4); bag y(x); x.insert(18); x.insert(19); x 4 2 984 18 19 ? [0] [1] [2] [3] y 4 984 Draw memory map on blackboard 500 x 512 y 964 y.data 984 x.data 1000 bottom change to x also changes y

Deep copy: providing your own copy constructor bag::bag(const bag& source) // Postcondition: The bag that has been constructed has the same items and capacity as source Questions on Implementation (homework!) do you need to check self-copy bag y(x); // never have bag y(y); do you need to delete old bag? Questions on Usage 4 different ways that copy constructor is used

Four common situations Declaration bag y(x); Declaration with Alternate Syntax bag y = x ; Returning an object from a function bag union(const bag& s1, const bag& s2); Value parameter is an object void temp_bag_copy(bag clone);

What’s missing? allocate dynamic memory via new, take care of the value semantics, ....?

De-allocation of dynamic memory Return an object’s dynamic memory to the heap when the object is no longer in use Where and How? – Two ways Take care of it yourself delete dynamic data of an object after you’re done with it let the program do it automatically destructor

bag::~bag() { delete [ ] data; } Destructor The primary purpose is to return an object’s dynamic memory to the heap, and to do other “cleanup” Three unique features of the destructor The name of the destructor is always ~ followed by the class name; No parameters, no return values; Activated automatically whenever an object becomes inaccessible Question: when this happens?

bag::~bag() { delete [ ] data; } Destructor Some common situations causing automatic destructor activation Upon function return, objects as local variables destroyed; Upon function return, objects as value parameters destroyed; when an object is explicitly deleted Question: shall we put destructor in how-to-use-a-bag documentation? No.

The Law of the Big Three Using dynamic memory requires the following three things all together a destructor a copy constructor (and of course an ordinary one) an overloaded assignment operator In other words, the three functions come in a set – either you need to write all three yourself, or you can rely on the compiler-supplied automatic versions of all the three.

What will happen if not? If we only have a constructor and a destructor, but do not provide a copy constructor and an overloaded assignment operator

Importance of the Law of Big-3 // constructor bag::bag(size_type init_cap) { data = new value_type[init_cap]; capacity = init_cap; used = 0; } bag *x, *y; x = new bag(4); y = new bag(5); x->insert(18); x->insert(19); *y = *x; delete x; y->insert(20); // destructor bag::~bag() { delete [ ] data; } Draw memory map on blackboard 500 x 512 y 964 y.data 984 x.data 1000 bottom Question: What will happen after executing lines 1 – 8?

Importance of the Law of Big-3 capacity used data bag *x, *y; x = new bag(4); y = new bag(5); x->insert(18); x->insert(19); *y = *x; delete x; y->insert(20); *x 4 984 ? [0] [1] [2] [3] *y ? 5 964 Draw memory map on blackboard 500 x 512 y 964 y.data 984 x.data 1000 bottom [0] [1] [2] [3] [4] // From implementation file bag2.cxx bag::bag(size_type init_cap) { data = new value_type[init_cap]; capacity = init_cap; used = 0; } allocate memory for objects (*x, *y) and their dynamic arrays

Importance of the Law of Big-3 capacity used data bag *x, *y; x = new bag(4); y = new bag(5); x->insert(18); x->insert(19); *y = *x; delete x; y->insert(20); *x 4 2 984 18 19 ? [0] [1] [2] [3] *y ? 5 964 Draw memory map on blackboard 500 x 512 y 964 y.data 984 x.data 1000 bottom [0] [1] [2] [3] [4] Insert two items in the dynamic array of object *x

Importance of the Law of Big-3 capacity used data bag *x, *y; x = new bag(4); y = new bag(5); x->insert(18); x->insert(19); *y = *x; delete x; y->insert(20); *x 4 2 984 18 19 ? [0] [1] [2] [3] *y ? 4 2 984 Draw memory map on blackboard 500 x 512 y 964 y.data 984 x.data 1000 bottom [0] [1] [2] [3] [4] lost memory automatic assignment only copies three variables (capacity, used and data) from *x to *y

Importance of the Law of Big-3 bag *x, *y; x = new bag(4); y = new bag(5); x->insert(18); x->insert(19); *y = *x; delete x; y->insert(20); dangling pointer *y ? 4 2 984 Draw memory map on blackboard 500 x 512 y 964 y.data 984 x.data 1000 bottom [0] [1] [2] [3] [4] lost memory bag::~bag() { delete [ ] data; } Deleting x will also delete the dynamic array of *x by calling the destructor

Importance of the Law of Big-3 bag *x, *y; x = new bag(4); y = new bag(5); x->insert(18); x->insert(19); *y = *x; delete x; y->insert(20); dangling pointer *y ? 4 2 984 Draw memory map on blackboard 500 x 512 y 964 y.data 984 x.data 1000 bottom [0] [1] [2] [3] [4] lost memory Your program crashes: *y needs its own copy of data !!!

Reading and Programming Assignments Putting pieces together bag2.h, bag2.cxx both in textbook and online Self-test exercises 16 - 23 After-class reading (string) Section 4.5, Self-Test 26- 32 (within exam scope) Programming Assignment 2 Due Feb 25 (Thu)! Assignment 3 is online , due March 11 (Thu) Next Class: Exam review March 04 Thursday: First Exam 4:00 – 5:40 pm