Download presentation
Presentation is loading. Please wait.
1
CISC/CMPE320 - Prof. McLeod
Winter 2013 CISC/CMPE320 2/22/2019 CISC/CMPE320 TAs are grading the SDD. Assignment 3 is due today at 7pm. Fall 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod
2
CISC/CMPE320 - Prof. McLeod
Today Using the Heap, Cont. - The “Big Three”. Fall 2018 CISC/CMPE320 - Prof. McLeod
3
CISC/CMPE320 - Prof. McLeod
“The Big Three” Again… Destructor: Frees all heap memory used by the object. Copy constructor: Initializes the object as a copy of the same type object supplied as a parameter. If you are using heap memory you will need to allocate and initialize each value. Assignment operator: Check to make sure that you are not assigning yourself. If so, do nothing. Free up the heap memory that is no longer needed. Copy the value of the argument supplied. Return *this. Fall 2018 CISC/CMPE320 - Prof. McLeod
4
CISC/CMPE320 - Prof. McLeod
Destructor Sounds sinister! If you have any attributes that use the heap, you must have a destructor that cleans them up: MyString::~MyString() { delete[] buffer; buffer = nullptr; } Fall 2018 CISC/CMPE320 - Prof. McLeod
5
CISC/CMPE320 - Prof. McLeod
Destructor, Cont. Invoked: For any local stack variables when execution reaches the end of the block in which they are declared. For any arguments when the end of the function is reached. Any temporary (named or unnamed) variables used in a statement when the statement is complete. If a variable stored on the heap is deleted (using delete). For all global and local static variables when main completes. When an object is deleted, for all attributes in the object. Fall 2018 CISC/CMPE320 - Prof. McLeod
6
CISC/CMPE320 - Prof. McLeod
Destructor, Cont. Invoked, cont.: When an object from a derived (child) class is deleted, for all attributes in the base (parent) class. (Aside – the destructor must be declared virtual. More later…) When an exception is thrown and, as a result, execution leaves the current block of code. If you don’t write your own destructor the system-generated one is just an empty destructor – rather useless… Only needed if any of your attributes use heap memory. Prevents memory leaks. Fall 2018 CISC/CMPE320 - Prof. McLeod
7
Aside – Deletion vs. Destruction
Deletion occurs when a pointer is passed to the delete operator. Destruction occurs whenever the destructor is invoked. A pointer is deleted, an object is destroyed. (Remember to set a pointer variable to nullptr after you have deleted it…) Fall 2018 CISC/CMPE320 - Prof. McLeod
8
MyString Class Example
Adapted from “Big C++” by Horstmann and Budd: Background: A “C-string” is just an array of char. A string literal will be stored on the stack. However the STL class string stores string contents on the heap, which simplifies any operation that changes the length of the string. Build our own class, MyString, which behaves like an abbreviated version of the STL class string. Mostly because it uses the heap. Fall 2018 CISC/CMPE320 - Prof. McLeod
9
Constructors – Overview
Not just a copy constructor: Default constructor. Conversion constructors. Parameterized constructors. And, when do constructors (and destructors) get called? This is important, since not all constructor calls are explicit and you cannot call the destructor directly at all. Fall 2018 CISC/CMPE320 - Prof. McLeod
10
MyString Class Constructors
MyString(); // Default constructor MyString(const char[]); // Simple constructor MyString(const MyString&); // Copy constructor MyString(int); // Conversion constructor MyString(char); // Conversion constructor Called when: Execution enters a block in which a MyString is declared. A variable of type MyString is placed on the stack. A global variable of type MyString is declared. The variable is placed in the globals memory before main starts. Fall 2018 CISC/CMPE320 - Prof. McLeod
11
CISC/CMPE320 - Prof. McLeod
Constructors, Cont. Called when, cont: A static local variable of type MyString is declared. The variable is placed in global memory. Whenever new MyString is invoked. The variable is created on the heap and the memory address stored in a pointer on the stack. An unnamed temporary variable is created to hold the return value of a function when it is of type MyString. An object is constructed that has an attribute of type MyString. A variable of a type extended from MyString is created. The constructor of the parent class is invoked by the constructor of the child class (no choice). Fall 2018 CISC/CMPE320 - Prof. McLeod
12
CISC/CMPE320 - Prof. McLeod
Constructors, Cont. Called when, cont: An unnamed variable is created when an overloaded operator is evaluated to hold the intermediate value. When a conversion is required in an mixed type expression using an overloaded operator. Fall 2018 CISC/CMPE320 - Prof. McLeod
13
MyString Class Constructors, Cont.
Can be invoked with: No argument at all (the default constructor). A string literal like “Hello!” (the simple constructor). An integer value (the conversion constructor). A char (another conversion constructor). Another MyString (the copy constructor). Fall 2018 CISC/CMPE320 - Prof. McLeod
14
CISC/CMPE320 - Prof. McLeod
Copy Constructor MyString::MyString(const MyString& right) { len = right.length(); buffer = new char[len + 1];// Using heap for (int i = 0; i < len; i++) buffer[i] = right[i]; buffer[len] = '\0'; } Creates a copy or clone of the supplied argument. Fall 2018 CISC/CMPE320 - Prof. McLeod
15
CISC/CMPE320 - Prof. McLeod
Copy Constructor, Cont. Invoked when: The MyString constructor is supplied with a MyString type argument. When a MyString is passed into a function by value (not as a reference – but it is better to pass a const reference…). The cloning carried out by the copy constructor prevents aliasing in these situations. Fall 2018 CISC/CMPE320 - Prof. McLeod
16
CISC/CMPE320 - Prof. McLeod
Copy Constructor, Cont. If you don’t write your own copy constructor, you get a system-defined copy constructor that carries out a simple, memberwise copy for each attribute in an object. In the case of an attribute stored on the heap, you can get multiple stack variables pointing (or aliased) to the same area of the heap – probably not what you want. Not a “deep copy” or “clone”, but more like a “shallow copy”. Fall 2018 CISC/CMPE320 - Prof. McLeod
17
MyString Class Example, Cont.
See String.h, String.cpp and TestString.cpp. Note that simple messages have been added to constructors and the destructor in this class so we can see when they are invoked. Check out when constructors are invoked. Then, check out the use of the copy constructor. Note that our MyString object is mutable! Fall 2018 CISC/CMPE320 - Prof. McLeod
18
MyString Class Example, Cont.
Create a MyString on the heap and then delete it. What is the difference between testD and testG in how they are stored? If I did not delete testG as I did, would it be destroyed when main completes? (see lastOne). What happens if I try to delete testG twice? Fall 2018 CISC/CMPE320 - Prof. McLeod
19
CISC/CMPE320 - Prof. McLeod
Assignment Operator MyString& MyString::operator=(const MyString& right) { if (this != &right) { // Check to see if assigning to self delete[] buffer; // Get rid of old buffer len = right.length(); buffer = new char[len + 1]; // Using heap for (int i = 0; i < len; i++) buffer[i] = right[i]; buffer[len] = '\0'; } return *this; // Return ref to self Fall 2018 CISC/CMPE320 - Prof. McLeod
20
Assignment Operator, Cont.
Almost exactly the same as the copy constructor. But since assignment is carried out on an existing object, it must clean up the heap first. Note how aliasing is prevented here, as well. Just in case you are dumb enough to assign a variable to itself you need to check so that this operation does not delete the buffer prematurely. Fall 2018 CISC/CMPE320 - Prof. McLeod
21
MyString Class Example, Cont.
Note concatenation operations provided by the overloaded += and + operators. Note that when an int or char is supplied that you get a call to the conversion constructor followed by a call to the destructor for the temporary, unnamed variable holding the converted value. Fall 2018 CISC/CMPE320 - Prof. McLeod
22
CISC/CMPE320 - Prof. McLeod
Memory Leak Demo Remove any heap deletion code from the destructor in the assignment 4 sample solution. Watch memory consumption in the task manager as the program runs! Fall 2018 CISC/CMPE320 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.