Reputation: 53
this is my first post to stack overflow. I'm creating a program to parse text into a linked list alphabetically, and to keep track of the count of each word. The program runs fine (by fine I mean 15 minutes which is obviously slow and not using a strong data structure) until my program returns and tries to deconstruct the dynamically allocated memory. Could someone help identify what pieces of this code I may need to adjust to avoid overflowing my stack?
template <class T>
void WordList<T>::destroyWord(WordList<T> *node)
{
WordList<T>* nodeD = NULL;
for(; node != NULL; )
{
if(node == NULL)
return;
else if(node->mNext != NULL)
{
nodeD = node;
node = node->mNext;
}
else
{
// We have found the node to delete.
//destroyWord(node->mNext);
if( node->mNext == NULL )
{
if( nodeD != NULL )
{
nodeD->mNext = NULL;
delete nodeD;
}
else
{
node = NULL;
}
}
nodeD = NULL;
}
}
// **********************************
// Delete the node at the root.
//delete node;
return;
}
Here is my revised code, thanks guys!....
template <class T>
void WordList<T>::destroyWord(WordList<T> *node)
{
node = node->mRootNode->mNext;
static WordList<T>* ptr = node;
for(; node != NULL && node->mNext != NULL; )
{
ptr = node->mNext;
delete (char*)node;
node = ptr;
}
delete (char*)ptr;
}
Upvotes: 0
Views: 119
Reputation: 10699
I would bet: your destructor most probably calls destroyWord
. We can see that the destroyWord
has a delete nodeD;
statement. nodeD
is type of WordList<T>
, so this will cause a destructor call on WordList<T>
, and that has a delete nodeD;
as we have seen. There is our infinite recursion.
Upvotes: 2