【发布时间】:2014-09-24 15:21:10
【问题描述】:
已经有几个标题相似的问题,但是当析构函数没有被调用时,我一直找不到一个出现段错误的问题。
我一直在编写一个模板化的双向链表(为了好玩和练习),到目前为止,它一直在正常工作(通过到处的插入和删除进行测试,测试边缘情况等)。
到目前为止,我还没有正确实现 RAII,所以现在实际使用是非常不安全(副本将是一个问题,除非你先清除列表,否则它会泄漏内存如果超出范围,就像一个上翻的水桶)。
我原来有一个空的析构函数:
template<class T>
dll<T>::~dll()
{}
一个查找函数:
template<class T>
int dll<T>::find_node(T data)
{
int index = 0;
dllnode<T>* current_node = end[0];
while (current_node)
{
if (current_node->data == data)
return index;
current_node = current_node->link[1];
index++;
}
return -1;
}
还有一个有效的 del_node 函数。
现在,我遇到的问题是,当我尝试清除析构函数中的任何数据列表时:
template<class T>
dll<T>::~dll()
{
while ( del_node(0) ){} //returns 0 if no nodes left to delete.
}
我的(未更改的)find_node() 函数会导致段错误。
由于代码的 only 更改在析构函数中,我“假设”它是相关的,但我不知道如何。析构函数永远不应该被调用,因为 dll 对象在堆栈上并且在 main 结束之前一直在作用域内。
使用空析构函数,代码按预期运行。
这是当前的示例代码:
dll_example.cpp:
#include <iostream>
#include <climits>
#include "dll.h"
void printout(dll<int> list)
{
std::cout<<"\nnumber of nodes: "<<list.get_node_count()<<std::endl;
std::cout<<"list contents:"<<std::endl;
list.print_list();
}
int main()
{
dll<int> list;
std::cout<<"created list" << std::endl;
for (int i = 0; i<10; i++)
{
int j = list.add_node(i, 5);
std::cout<<"added a node, value: "<<i<<", index: "<< j << std::endl;
}
printout(list);
std::cout<<"\nfinding '8'" << std::endl; //Prints this line
int index = list.find_node(8);
std::cout<<"deleting '8' (index: "<<index<<")"<< std::endl; //never gets this far
list.del_node(index);
printout(list);
std::cout<<"\ndeleting #1" << std::endl;
list.del_node(1);
printout(list);
std::cout<<"\ndeleting #0" << std::endl;
do{ //manually delete all entries including
//a del_node() call on empty list (works)
printout(list);
std::cout<<"deleting #0" << std::endl;
}while(list.del_node(0));
printout(list);
}
错误输出:
$ ./dll_example.exe
created list
added a node, value: 0, index: 0
added a node, value: 1, index: 1
added a node, value: 2, index: 2
added a node, value: 3, index: 3
added a node, value: 4, index: 4
added a node, value: 5, index: 5
added a node, value: 6, index: 5
added a node, value: 7, index: 5
added a node, value: 8, index: 5
added a node, value: 9, index: 5
number of nodes: 10
list contents:
0, 1, 2, 3, 4, 9, 8, 7, 6, 5,
finding '8'
Segmentation fault (core dumped)
空析构函数输出:
...
list contents:
0, 1, 2, 3, 4, 9, 8, 7, 6, 5,
finding '8'
deleting '8' (index: 6)
number of nodes: 9
list contents:
0, 1, 2, 3, 4, 9, 7, 6, 5,
...
【问题讨论】:
-
“和一个有效的 del_node”函数 它实际上做了什么?
-
您正在将一个列表按值传递给
printout。这将使用您的复制构造函数和您的析构函数。三法则不会原谅,三法则不会忘记(当你从另一个角度看时,它就变成了五法则。)尊重它。 -
@n.m.我不敢相信我错过了。刚刚更改为
printout(list<int>& list)并且它可以工作 - 这将是因为现在缺少复制构造函数,对吗? (facepalm,我确实说过这是为了练习,对吧?)。将其发布为答案,我会接受。 :)