winter 2015cisc/cmpe320 - prof. mcleod1 cisc/cmpe320 assn 4 posted shortly. demonstrate a memory...

12

Click here to load reader

Upload: paulina-nelson

Post on 17-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to

Winter 2015 CISC/CMPE320 - Prof. McLeod 1

CISC/CMPE320

• Assn 4 posted shortly.

• Demonstrate a memory leak problem using the assignment 4 solution.

• Back to the “Big Three” – using the String class example.

• Next: Back to Software Engineering for a while.

Page 2: Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to

Memory Leak Demo

• I will 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!

Winter 2015 CISC/CMPE320 - Prof. McLeod 2

Page 3: Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to

Winter 2015 CISC/CMPE320 - Prof. McLeod 3

Back to the String 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, String, which behaves like an abbreviated version of the STL class string.

• Mostly because it uses the heap.

Page 4: Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to

Winter 2015 CISC/CMPE320 - Prof. McLeod 4

String Class Constructors

String(); // Default constructorString(const char[]); // Simple constructorString(const String&); // Copy constructorString(int); // Conversion constructor

• Called when:– Execution enters a block in which a String is declared.

A variable of type String is placed on the stack.– A global variable of type String is declared. The

variable is placed in the globals memory before main starts.

Page 5: Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to

Winter 2015 CISC/CMPE320 - Prof. McLeod 5

Copy Constructor

String::String(const String& right) {len = right.length();buffer = new char[len + 1];for (int i = 0; i < len; i++)

buffer[i] = right[i];buffer[len] = '\0';

}

• Creates a copy or clone of the supplied argument.

Page 6: Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to

Winter 2015 CISC/CMPE320 - Prof. McLeod 6

Copy Constructor, Cont.

• Invoked when:– The String constructor is supplied with a String type

argument.– When you assign one String to another using =.– When a String is passed into a function as a value

parameter (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.

Page 7: Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to

Winter 2015 CISC/CMPE320 - Prof. McLeod 7

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

Page 8: Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to

Winter 2015 CISC/CMPE320 - Prof. McLeod 8

String 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 String object is mutable!

Page 9: Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to

Winter 2015 CISC/CMPE320 - Prof. McLeod 9

String Class Example, Cont.

• Create a String 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?

Page 10: Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to

Winter 2015 CISC/CMPE320 - Prof. McLeod 10

Assignment Operator

String& String::operator=(const String& right) {if (this != &right) {

delete[] buffer;len = right.length();buffer = new char[len + 1];for (int i = 0; i < len; i++)

buffer[i] = right[i];buffer[len] = '\0';

}return *this;

}

Page 11: Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to

Winter 2015 CISC/CMPE320 - Prof. McLeod 11

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 assign a variable to itself you need to check so that this operation does not delete the buffer prematurely.

Page 12: Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to

Winter 2015 CISC/CMPE320 - Prof. McLeod 12

String Class Example, Cont.

• Note concatenation operations provided by the overloaded += operator.

• Note that when an int 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 int.

• Can you explain what happens when you try to concatenate a char to a String? Why does it work at all?