[英]Stack overflow with my destructor
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.
该程序运行良好(罚款我的意思是15分钟,显然很慢,并没有使用强大的数据结构),直到我的程序返回并尝试解构动态分配的内存。 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;
}
I would bet: your destructor most probably calls destroyWord
. 我敢打赌:你的析构函数最有可能称为
destroyWord
。 We can see that the destroyWord
has a delete nodeD;
我们可以看到
destroyWord
有一个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;
nodeD
是WordList<T>
类型,因此这将导致对WordList<T>
的析构函数调用,并且具有delete nodeD;
as we have seen. 正如我们所看到的。 There is our infinite recursion.
有无限的递归。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.