The full code is located here: https://gist.github.com/4521540
It's a dummy List in C++. My concern is with freeing up memory.
It doesn't crash when I run my code. It looks like my if/else covers everything.
- It starts by deleting the second item if there is. That's what the while loop does.
- If there is only one present (or left with one), delete
startNode.
startNode is a pointer points to the first item in the list.
endNode always points to the last item in the list.
Am I deleting the pointer or the underlying object?
I do mostly Python. Last time I wrote a serious C++ homework was about 2 years ago in my algorithm class so I really can't remember everything off my head.
~List()
{
// there is at least one item
if(startNode != 0)
{
// release memory starting from the second item
ListNode *current, *soon;
current = this->startNode->next;
while(current != 0) // if there are at least two items
{
/* When there is no more items after current,
* delete current and leave.
* Otherwise, free up current and move on to
* the next item.
*/
if(current->next != 0)
{
soon = current->next;
delete current;
current = soon;
}
else
{
delete current;
break;
}
}
}
delete this->startNode;
delete this->endNode;
}
Also, do I need to delete the myList in my main program? I think when I exit the program, the destructor is automatically called.
Thanks for looking!