Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to."— Presentation transcript:

1 Winter 2015CISC/CMPE320 - Prof. McLeod1 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.

2 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 2015CISC/CMPE320 - Prof. McLeod2

3 Winter 2015CISC/CMPE320 - Prof. McLeod3 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.

4 Winter 2015CISC/CMPE320 - Prof. McLeod4 String Class Constructors String(); // Default constructor String(const char[]); // Simple constructor String(const String&); // Copy constructor String(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.

5 Winter 2015CISC/CMPE320 - Prof. McLeod5 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.

6 Winter 2015CISC/CMPE320 - Prof. McLeod6 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.

7 Winter 2015CISC/CMPE320 - Prof. McLeod7 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”.

8 Winter 2015CISC/CMPE320 - Prof. McLeod8 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!

9 Winter 2015CISC/CMPE320 - Prof. McLeod9 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?

10 Winter 2015CISC/CMPE320 - Prof. McLeod10 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; }

11 Winter 2015CISC/CMPE320 - Prof. McLeod11 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.

12 Winter 2015CISC/CMPE320 - Prof. McLeod12 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?


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

Similar presentations


Ads by Google