Lab 5 - Copy Constructors and Assignment Operators

Due: 5:00pm on Wednesday

The purpose of this lab is to see why copy constructors and assignment operators are vital for class which have dynamic array member variables.

Copy the class list.cpp to your directory using wget or cut/paste (see Labs 1 and 2 for these commands). This is a class which has a dynamic double array, but does not have a copy constructor or assignment operator defined. Because of this, any time the copy constructor or assignment operator would be needed, the default action of copying the base address of the array over occurs, so multiple objects point to the same array.

Compile and run the program as given using the following commands:

g++ -g -o lab5 list.cpp
./lab5
Your output should appear similar to the following (without the color coding, which I've added to facilitate the discussion below, also your addresses will differ from the addresses given):
Report and value of a (default constructor):
array pointer address=0x0  array size=0


Report and value of b (constructor w/ array 1):
array pointer address=0x502010  array size=4
4.5 6.5 2.3 1.2

Report and value of c (constructor w/ array 2):
array pointer address=0x502040  array size=6
5.2 4.3 7.1 8.5 3.7 2.8

Report and value for the clone of c (copy constructor):
array pointer address=0x502040  array size=6
5.2 4.3 7.1 8.5 3.7 2.8

Report and value of a after using a = c (assignment operator):
array pointer address=0x502040  array size=6
5.2 4.3 7.1 8.5 3.7 2.8

** DEBUG ** Report for tmp in + operator:
array pointer address=0x502080  array size=10

Report and value of a after concatenating arrays (assignment operator):
array pointer address=0x502080  array size=10
4.5 6.5 2.3 1.2 5.2 4.3 7.1 8.5 3.7 2.8
*** glibc detected *** double free or corruption (fasttop): 0x0000000000502040 ***
Aborted
Note the addresses in red are all the same, even though they are outputing three seperate objects (c, clone and a). This is because the copy constructor and assignment operator are not defined, so the base address of c was just copied over to a and clone, rather than allocating new arrays for a and clone.

Likewise, the addresses in blue are the same because these functions do not exist. In this case, return temp calls the copy constructor to make the anonymous, unnamed object to pass back to main. Since the copy constructor is not defined, the base address is just copied verbatim into the anonymous object. Then in main, since the assignment operator is not defined, the base address is copied again into a. This is BAD because temp was destroyed when the function ended and the memory at 0x502080 has been marked as free. a is referencing memory that could be handed out at any moment to another user. If you see random element values appearing in the element output, this is an indication that the memory has indeed been given to another user to use.

Finally, note the text that appears in green at the end of the program. When main() ends, all the objects created in main (a, b, c and clone) are destroyed. Since a points to memory that was already deleted when the temporary object in the + operator was destroyed and b, c and clone all point to the same object, you will end up trying to call delete [] array on the same memory address more than once. When this happens, the system aborts your code with this error message about "double free" (free is the C version of delete). This is another indication that the copy constructor and assignment operators have not properly been defined.

Assignment

Your assignment is to fix all of these problems by defining the assignment operator and copy constructor, as discussed in class. Recall that both functions will allocate an array based on the size of the source array and copy the elements from the source array into their newly allocated space. Also remember that the assignment operator must deal with the possibility of being assigned to itself and must delete any existing array before allocating a new one.

When you have properly defined the assignment operator and copy constructor, recompile the code and run it again. You should now see different memory addresses for all the spots highlighted in red and blue above. Additionally, the "double delete" error message in green should be gone.

Email me the source code for your corrected list.cpp that has the assignment operator and copy constructor defined.