This is a follow up to a question I asked earlier today regarding the appropriation of directly calling a class’ destructor.
I’m creating my own List for an assignment. I have overloaded the assignment, square brackets, in and out stream operators and have the basic adding, removing and printing to screen functionality.
My next step is to have the List delete and release the memory of all of its nodes, which I am having trouble with.
I have the List write to the screen its current state after performing its operations, as follows:
void main()
{
// Create instance of list
List aList(true);
List bList(true);
aList.Add(1);
aList.Add(2);
bList.Add(2);
cout << "aList: " << aList;
cout << "bList: " << bList;
aList = bList;
cout << "aList: " << aList;
cout << "bList: " << bList;
aList.Add(3);
cout << "aList: " << aList;
cout << "bList: " << bList;
int i = 0; cin >> i;
}
Here is my overloaded out stream:
ostream &operator<<(ostream &stream, List theList)
{
stream << "There are " << theList._totalNodes << " nodes in the list." << endl;
for(int i = 0; i < theList._totalNodes; i++)
{
stream << "Node #" << i << " holds the data: " << theList[i] << endl;
}
return stream;
}
Looking at the command prompt window after this is used “Destructor called” is also printed underneath it.
The List destructor:
List::~List()
{
cout << "Destructor called" << endl;
// List::Destroy();
}
The overloaded assignment operator also seems to call the destructor, which doesn’t surprise me. However, the others do.
List &List::operator=(List &aList)
{
// Make sure not the same object trying to be copied
if(this != &aList)
{
// Perform deep copy
// Delete old pointers in redundant list, only if they exist already
if(_head)
{
delete _head, _tail, _current, _del;
_totalNodes = 0; // Reset total nodes
}
// Now set new pointers copied from other list
_head = NULL;
_tail = _head;
_current = _head;
_del = _head;
// Now loop through all nodes in other list and copy them to this
// Reset pointer in other list
aList.Reset();
for(int i = 0; i < aList._totalNodes; i++)
{
Add(aList.Current()->_data);
aList.Next();
}
// Reset other list's pointer
aList.Reset();
Reset(); // Reset our pointer
}
return *this; // Return for multi assignment
}
I also have the destructor to a List write “Destructor called” when it’s called and noticed that it is called after every use of one of its methods.
Why is this? I assumed the destructor is called when the object is no longer needed, i.e. deleted.
Furthermore, when stepping through my code I have noticed that when a pointer is deleted the memory address it is pointing to isn’t being nullified. Do I have to manually NULL a pointer after it is deleted?
It’s because you took your List by value- that is, you copied it. You need to take it by const reference- that is,
ostream &operator<<(ostream &stream, const List& theList)Furthermore, your assignment operator should take a const reference, not a non-const ref.
I also don’t think that this
delete _head, _tail, _current, _del;does what you think it does. It just deletes
_deland none of the others.You should use a recursively self-owning design, where each node deletes the next one in the chain. Edit: As this was unclear, I’m going to post a brief sample.
Notice how I never deleted anything-
std::auto_ptrdid everything for me, and manipulating the list and it’s nodes became much easier, becausestd::auto_ptrmanages all of the memory involved. This list doesn’t even need a custom destructor.I suggest that you read up heavily on
std::auto_ptr, it has some surprises in it’s interface, like the “copy” constructor and assignment operator actually move, that you are going to want to know about before using the class. I wish I could start usingstd::unique_ptrinstead all of the time, which would make life so much easier.