Copy the class list.cpp to your current directory using
cp /usr/users/mdanfor/public_html/cs222-w07/list.cpp .Compile this program exactly as given and run the executable. Notice that the pointer addresses are copied over when assignment or cloning occurs. For example, after
clone = DoubleList(c)
, both c
and clone
are pointing to the same chunk of memory. This means
if one is deleted, the other would be pointing to "free" memory space which
can be given to someone else to use. If the system is under load, you may
notice random values in the last line of output corresponding to the values
stored in a
after a = b + c;
. This is because the
+ operator creates a temporary DoubleList to store the new concatenated
array. The temporary variable is deleted after a = b + c;
, but
a
continues to point to the allocated space. The system thinks
that memory is free, so it MAY allocate it to another program in the short
space between a = b + c;
and cout << a
. If that
occurs, the other program that has allocated the memory can write whatever
it wants into that memory space, which can lead to random values being
displayed when you call cout << a
.
To prevent this from occuring, you need to define a copy constructor and an assignment operator. I have given the prototypes for those functions in the code, but commented them out so you could run the above experiment. Both functions will do the same basic tasks:
Compile the new program with the copy constructor and assignment operators
defined. If you have defined them correctly, you should now see that the
array pointer address changes for clone
and a
when
it did not change before.
Email the source code for this final program to me.