<bdo id='H4weG'></bdo><ul id='H4weG'></ul>
  • <tfoot id='H4weG'></tfoot>
    <legend id='H4weG'><style id='H4weG'><dir id='H4weG'><q id='H4weG'></q></dir></style></legend>
  • <small id='H4weG'></small><noframes id='H4weG'>

  • <i id='H4weG'><tr id='H4weG'><dt id='H4weG'><q id='H4weG'><span id='H4weG'><b id='H4weG'><form id='H4weG'><ins id='H4weG'></ins><ul id='H4weG'></ul><sub id='H4weG'></sub></form><legend id='H4weG'></legend><bdo id='H4weG'><pre id='H4weG'><center id='H4weG'></center></pre></bdo></b><th id='H4weG'></th></span></q></dt></tr></i><div id='H4weG'><tfoot id='H4weG'></tfoot><dl id='H4weG'><fieldset id='H4weG'></fieldset></dl></div>

      1. unique_ptr 链表的堆栈溢出

        Stack overflow with unique_ptr linked list(unique_ptr 链表的堆栈溢出)
      2. <legend id='jGcb0'><style id='jGcb0'><dir id='jGcb0'><q id='jGcb0'></q></dir></style></legend>
            <tfoot id='jGcb0'></tfoot>

              <small id='jGcb0'></small><noframes id='jGcb0'>

                <i id='jGcb0'><tr id='jGcb0'><dt id='jGcb0'><q id='jGcb0'><span id='jGcb0'><b id='jGcb0'><form id='jGcb0'><ins id='jGcb0'></ins><ul id='jGcb0'></ul><sub id='jGcb0'></sub></form><legend id='jGcb0'></legend><bdo id='jGcb0'><pre id='jGcb0'><center id='jGcb0'></center></pre></bdo></b><th id='jGcb0'></th></span></q></dt></tr></i><div id='jGcb0'><tfoot id='jGcb0'></tfoot><dl id='jGcb0'><fieldset id='jGcb0'></fieldset></dl></div>

                  <tbody id='jGcb0'></tbody>
                  <bdo id='jGcb0'></bdo><ul id='jGcb0'></ul>
                  本文介绍了unique_ptr 链表的堆栈溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经转换了以下链表结构

                  I've converted the following linked list struct

                  struct node {
                    node* next;
                    int v;
                  };
                  

                  进入 c++11 版本 - 不使用指针.

                  into a c++11 version - that is not using the pointers.

                  struct node {
                    unique_ptr<node> next;
                    int v;
                  };
                  

                  添加、删除元素和遍历工作正常,但是当我插入大约 100 万个元素时,在调用头节点的析构函数时会出现堆栈溢出.

                  Adding, removing elements and traversing works fine, however when I insert roughly 1mil elements, I get a stack overflow when when the destructor of the head node is called.

                  我不确定我做错了什么.

                  I'm not sure what I'm doing wrong.

                  {
                    node n;
                  
                    ... add 10mill elements
                  
                  } <-- crash here
                  

                  推荐答案

                  你没有做错任何事.

                  当您创建包含 1000 万个元素的列表时,为每个节点分配 make_unique 一切正常(当然,数据不在堆栈中,也许第一个节点除外!).

                  When you create your list of 10 millions elements, allocation each node with make_unique everything is fine (Of course the data is not on the stack, except perhaps the first node !).

                  问题是当你去掉列表的头部时:unique_ptr 将负责删除它拥有的下一个节点,它也包含一个 unique_ptr 将负责删除下一个节点......等等......

                  The problem is when you you get rid of the head of your list: unique_ptr will take care of the deleting the next node that it owns, which also contains a unique_ptr that will take care of deleting the next node... etc...

                  所以最后这 1000 万个元素会被递归删除,每次递归调用都会占用堆栈上的一些空间.

                  So that in the end the 10 millions elements get deleted recursively, each recursive call taking some space on the stack.

                  这篇关于unique_ptr 链表的堆栈溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                • <tfoot id='nHw1I'></tfoot>

                  <small id='nHw1I'></small><noframes id='nHw1I'>

                  <legend id='nHw1I'><style id='nHw1I'><dir id='nHw1I'><q id='nHw1I'></q></dir></style></legend>

                    <i id='nHw1I'><tr id='nHw1I'><dt id='nHw1I'><q id='nHw1I'><span id='nHw1I'><b id='nHw1I'><form id='nHw1I'><ins id='nHw1I'></ins><ul id='nHw1I'></ul><sub id='nHw1I'></sub></form><legend id='nHw1I'></legend><bdo id='nHw1I'><pre id='nHw1I'><center id='nHw1I'></center></pre></bdo></b><th id='nHw1I'></th></span></q></dt></tr></i><div id='nHw1I'><tfoot id='nHw1I'></tfoot><dl id='nHw1I'><fieldset id='nHw1I'></fieldset></dl></div>

                            <tbody id='nHw1I'></tbody>
                            <bdo id='nHw1I'></bdo><ul id='nHw1I'></ul>