Loops like the copy constructor is doing a shallow copy instead of a deep copy.
Your non-copy constructor is allocating a buffer. We'll call this buffer "foo" even though it's nameless.
Your 'c' object has a pointer "arrayPtr" which points to this "foo" buffer.
Now.. when you create your 'e' object, it is copy constructed. Your copy constructor does not allocate a new buffer... but instead just has e.arrayPtr set to c.arrayPtr. Since c.arrayPtr points to "foo", this means that e.arrayPtr will also point to "foo".
The problem now is when 'c' goes out of scope and its destructor is run, it will delete[] arrayPtr. Remember that delete does not delete the
pointer... but rather it deletes
what the pointer points to. So in this case, it will delete "foo".
Now that "foo" is deleted, it no longer exists. This is a problem because your 'e' object is still pointing to it, and it thinks it's still there. So now when 'e' is destroyed, and its destructor is run... it attempts to delete "foo"
again, which will cause your program to explode because you can't delete something that no longer exists.
EDIT:
a simpler example:
1 2 3 4 5 6 7 8 9 10 11
|
int* a = new int; // 'new' creates a new int ("foo") and returns a pointer to it.
int* b = a; // your "copy constructor" is just assigning a pointer
// at this point, both 'a' and 'b' point to the same memory: "foo"
delete a; // since 'a' points to foo, this deletes "foo". Foo no longer exists
// since 'b' pointed to foo, and since foo no longer exists.. 'b' now is a bad pointer
// (it points to garbage)
delete b; // trying to delete garbage -- EXPLODE
|