<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 链表的堆栈溢出

        时间:2023-10-18
      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 链表的堆栈溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为链表创建复制构造函数 下一篇:随机插入/删除的综合向量与链表基准

                  相关文章

                • <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>
                        <bdo id='nHw1I'></bdo><ul id='nHw1I'></ul>