- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是一名学习 C++ 的 SWE,并使用 std::unique_ptr 作为对 head
和 next
的引用构建了一个简单的 LinkedList 类。这是基本结构:
template <class T>
struct LinkedListNode {
T value;
std::unique_ptr<LinkedListNode<T>> next;
// For debugging.
~LinkedListNode() {
std::cout << "destructed for value " << this->value << std::endl;
}
};
template <class T>
struct LinkedList {
std::unique_ptr<LinkedListNode<T>> head;
};
使用智能指针,我希望当 LinkedList 实例被删除或超出范围时,head
将被删除,并且每个 next
节点将被递归删除也是。
而这正是发生的事情。然而,当处理非常长的列表(~20M 节点)时,令人惊讶的是它仍然可以正常工作。它不应该因为堆栈溢出而崩溃吗?
为了非常粗略地估计我的操作系统堆栈的大小,我编写了以下脚本:
int main() {
struct s {
static void p(int i) {
std::cout << i << std::endl;
p(i+1);
};
s::p(0);
}
它在迭代次数 ~175K 时崩溃,比我之前能够解除分配的 20M 节点少得多。到底是怎么回事?我是否遗漏了 unique_ptr 的工作方式?
最佳答案
在你的例子中你确实有递归,它没有达到堆栈溢出的真正原因可能是因为它是一个 tail-call recursion可以优化为迭代解决方案。
使用这段代码:
struct Node
{
int value;
std::unique_ptr<Node> next;
Node(int value, Node* next) : value(value), next(next) { }
~Node()
{
if (value == 0)
cout << "foo" << endl;
}
};
int main()
{
Node* node = new Node(0, nullptr);
for (int i = 1; i <= 5; ++i)
node = new Node(i, node);
delete node;
return 0;
}
通过在 cout
语句上放置一个断点并检查堆栈跟踪,您可以清楚地看到该行为是递归的:
还显示了行为 here通过使用基本析构函数来跟踪 ~Node()
何时返回。
由于 next
节点必须在从析构函数返回之前被销毁,这导致再次调用 ~Node()
。通过使用原始指针并直接在析构函数中删除下一个指针,此行为将是相同的,这确实已经得到回答here .
关于C++ smart_ptr 不会导致堆栈溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33595530/
提升智能指针can be used with polymorphism,但是如何将子类转换回指针? using namespace boost; // ... shared_ptr a_ptr(new
有什么方法可以从函数中销毁 std::shared_ptr 吗?在下面的示例中,如果我在 main() 中将 sptr 设置为 nullptr,它会“破坏”对象,因此调用它是 Print( ) 失败。
我想知道这个例子是否会导致段错误,因为对象的 dtor 被调用了,我仍然持有指向对象属性的 shared_ptr。 struct foo{ std::shared_ptr const bar
我是一名学习 C++ 的 SWE,并使用 std::unique_ptr 作为对 head 和 next 的引用构建了一个简单的 LinkedList 类。这是基本结构: template stru
可以boost::smart_ptr比如scoped_ptr和shared_ptr可以用在多态中吗? class SomeClass { public: SomeClass() {
我正在尝试建立哪些数据成员基元/对象(超出 T* 指针)boost(或 std,如果它更容易的话) shared_ptr 有,当 shared_ptr 用于访问底层堆时,它将与 T* 指针位于同一 L
作为一个试图理解智能指针的 C++ 新手。我写了下面的代码来检查。 它确实编译并运行了,但我期望我的类的析构函数被调用并从析构函数打印 cout,但它没有。 我们是否需要重载用户定义类中的任何函数,以
鉴于以下情况: class Curve { public: typedef boost::shared_ptr Pointer; // ... private: // ... }; cla
我目前正在做很多关于异常安全的事情。 (Herb Sutter 的 Exceptional C++、C++ FAQ Lite 等) 特别是,我想编写并理解 C++ FAQ Lite 的引用计数示例,但
因此,我只想将智能指针与boost分开,以便在我的项目中使用,因此我被引导使用bcp实用程序。 今天,我将其编译并执行bcp smart_ptr to_copy_to_my_project/。 结果:
我在一个容器中使用 boost::scoped_array,我想进行写时复制,但我担心 scoped_array 无法工作。哪个 boost::smart_ptr 容器最接近 cow-safe 作用域
我找不到一个完整的例子来说明如何消除 shared_ptr 之间的强循环引用。 问题是如何使用 weak_ptr 来“关闭”通用元素链并使用 weak_ptr 访问“下一个”元素。 谢谢。 编辑: 例
scoped_ptr、shared_ptr等boost::smart_ptr可以用在std::map等std容器中吗? class SomeClass { std::map > a_map;
我正在尝试序列化我无法使用 boost::serialization 修改的自定义类,并且我需要将逻辑/计算代码与序列化部分分开。它有一些我必须序列化的 protected 和私有(private)字
我在boost::smart_ptr中找到了以下自旋锁代码: bool try_lock() { return (__sync_lock_test_and_set(&v_, 1) == 0);
我正在关注文件 boost/smart_ptr/detail/operator_bool.hpp 并遇到以下我不理解的代码片段 typedef T * this_type::*unspecified_
我正在尝试在我的 Windows 7 笔记本电脑上轻松开发一个 websocket++ 服务器(vi 还不太好,不想经常使用 ftp)。我将在最后迁移到 Ubuntu 12.10。 我正在使用 Vis
我是一名优秀的程序员,十分优秀!