- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
已经有一些标题相似的问题,但我无法找到一个在未调用析构函数时出现段错误的问题。
我一直在写一个模板化的双向链表(为了好玩和练习),到目前为止,它一直在正常工作(测试了所有地方的插入和删除,测试边缘情况等)。
到目前为止我还没有正确实现 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()
函数导致段错误。
由于对代码的唯一更改是在析构函数中,我“假设”它是相关的,但我不知道如何。永远不应该调用析构函数,因为 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,
...
最佳答案
void printout(dll<int> list)
list
是printout
的本地内容。析构函数将在返回时调用。
printout(list);
这将使用复制构造函数来传递参数。
如果您的复制构造函数与析构函数不匹配,就会发生坏事。
如果您想在不实现复制构造函数和复制赋值运算符的情况下使用您的类,最好将它们声明为 private
而无需实现它们:
template<class T>
class dll {
private:
dll(dll&); // no implementation
void operator=(dll&); // no implementation
...
一旦你不小心使用了拷贝构造函数,编译器就会对你大吼大叫。
关于c++ - 添加析构函数逻辑导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26020686/
我开始考虑在我 future 的项目或重构中实现控制反转容器,我想知道在正确设计依赖项时哪些原则(除了 GoF 模式)可能需要牢记在心。假设我需要构建一个简单的控制台应用程序,如果它可以访问互联网,它
假设我有一个 RxC contingency table 。这意味着有 R 行和 C 列。我想要一个维度为 RC × (R + C − 2) 的矩阵 X,其中包含行的 R − 1 “主效应”以及列的
我正在尝试使用 DKMS 为正在运行的内核 (4.4) 构 build 备树覆盖。我天真的 Makefile 如下: PWD := $(shell pwd) dtbo-y += my-awsome-o
我有一个 sencha touch 项目。我是用 phonegap 2.9 构建的,并且可以正常工作 device.uuid 返回到设备 ID。当我尝试使用 3.1 device.uuid 构建时抛出
我在安装了 Xcode 4.5.1 的 Mt Lion 上运行。 默认情况下,当我构建并部署到 iOS 5.1 设备时,显示会在我旋转设备时旋转,但当我部署到 iOS 6 模拟器或运行 iOS 的 i
我正在尝试使用 Google Analytics Reporting API v4 构建多折线图。 一张图表,其中我按每天的 session 计数为每个设备(台式机/平板电脑/移动设备)设置了一条线。
我一生都无法使用 xcode 组织者“自动设备配置”中的“团队配置配置文件”在 xcode 4.0.1 中将我的应用程序构建到我的 iPad 上。 该应用程序完美地构建到模拟器,但当我构建到 iPad
我是一名优秀的程序员,十分优秀!