久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <legend id='uYYub'><style id='uYYub'><dir id='uYYub'><q id='uYYub'></q></dir></style></legend>
  • <i id='uYYub'><tr id='uYYub'><dt id='uYYub'><q id='uYYub'><span id='uYYub'><b id='uYYub'><form id='uYYub'><ins id='uYYub'></ins><ul id='uYYub'></ul><sub id='uYYub'></sub></form><legend id='uYYub'></legend><bdo id='uYYub'><pre id='uYYub'><center id='uYYub'></center></pre></bdo></b><th id='uYYub'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='uYYub'><tfoot id='uYYub'></tfoot><dl id='uYYub'><fieldset id='uYYub'></fieldset></dl></div>

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

      <tfoot id='uYYub'></tfoot>

          <bdo id='uYYub'></bdo><ul id='uYYub'></ul>
      1. 誰刪除了在“新建"期間分配的內存?構造函數

        Who deletes the memory allocated during a quot;newquot; operation which has exception in constructor?(誰刪除了在“新建期間分配的內存?構造函數中有異常的操作?)
            <tfoot id='Iodju'></tfoot>
              <bdo id='Iodju'></bdo><ul id='Iodju'></ul>

              1. <i id='Iodju'><tr id='Iodju'><dt id='Iodju'><q id='Iodju'><span id='Iodju'><b id='Iodju'><form id='Iodju'><ins id='Iodju'></ins><ul id='Iodju'></ul><sub id='Iodju'></sub></form><legend id='Iodju'></legend><bdo id='Iodju'><pre id='Iodju'><center id='Iodju'></center></pre></bdo></b><th id='Iodju'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Iodju'><tfoot id='Iodju'></tfoot><dl id='Iodju'><fieldset id='Iodju'></fieldset></dl></div>

                  <tbody id='Iodju'></tbody>
                  <legend id='Iodju'><style id='Iodju'><dir id='Iodju'><q id='Iodju'></q></dir></style></legend>

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

                  本文介紹了誰刪除了在“新建"期間分配的內存?構造函數中有異常的操作?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我真的不敢相信我找不到明確的答案...

                  在使用 new 運算符初始化的情況下,如何釋放 C++ 類構造函數拋出異常后分配的內存.例如:

                  class 廢話{上市:廢話(){拋出哎呀";}};無效主(){廢話* b = NULL;嘗試{b = new Blah();}抓住 (...){//現在怎么辦?}}

                  當我嘗試這樣做時,catch 塊中的 b 為 NULL(這是有道理的).

                  在調試時,我注意到控制在命中構造函數之前進入內存分配例程.

                  MSDN 網站上的這一點似乎證實了這一點::><塊引用>

                  當new用于分配內存時對于 C++ 類對象,對象的構造函數在內存之后被調用已分配.

                  所以,記住局部變量 b 永遠不會被賦值(即在 catch 塊中為 NULL)你如何刪除分配的內存?

                  如果能得到一個跨平臺的答案也很好.即,C++ 規范是怎么說的?

                  澄清:我不是在談論類在 c'tor 中自己分配內存然后拋出的情況.我很感激在這些情況下不會調用 d'tor.我說的是用于分配 THE 對象的內存(在我的例子中是 Blah).

                  解決方案

                  你應該參考類似的問題 此處 和此處.基本上,如果構造函數拋出異常,您就可以安全地再次釋放對象本身的內存.雖然,如果在構造函數期間已經聲明了其他內存,則在離開構造函數之前,您需要自行釋放它,但有異常.

                  對于誰刪除內存的問題,答案是 new-operator 背后的代碼(由編譯器生成).如果它識別出離開構造函數的異常,它必須調用類成員的所有析構函數(因為那些已經在調用構造函數代碼之前成功構造)并釋放它們的內存(可以與析構函數調用一起遞歸完成,很可能通過對它們調用適當的delete)以及釋放為此類本身分配的內存.然后它必須將捕獲的異常從構造函數重新拋出給 new 的調用者.當然,可能還有更多工作要做,但我無法從腦海中提取所有細節,因為它們取決于每個編譯器的實現.

                  I really can't believe I couldn't find a clear answer to this...

                  How do you free the memory allocated after a C++ class constructor throws an exception, in the case where it's initialised using the new operator. E.g.:

                  class Blah
                  {
                  public:
                    Blah()
                    {
                      throw "oops";
                    }
                  };
                  
                  void main()
                  {
                    Blah* b = NULL;
                    try
                    {
                      b = new Blah();
                    }
                    catch (...)
                    {
                      // What now?
                    }
                  }
                  

                  When I tried this out, b is NULL in the catch block (which makes sense).

                  When debugging, I noticed that the conrol enters the memory allocation routine BEFORE it hits the constructor.

                  This on the MSDN website seems to confirm this:

                  When new is used to allocate memory for a C++ class object, the object's constructor is called after the memory is allocated.

                  So, bearing in mind that the local variable b is never assigned (i.e. is NULL in the catch block) how do you delete the allocated memory?

                  It would also be nice to get a cross platform answer on this. i.e., what does the C++ spec say?

                  CLARIFICATION: I'm not talking about the case where the class has allocated memory itself in the c'tor and then throws. I appreciate that in those cases the d'tor won't be called. I'm talking about the memory used to allocate THE object (Blah in my case).

                  解決方案

                  You should refer to the similar questions here and here. Basically if the constructor throws an exception you're safe that the memory of the object itself is freed again. Although, if other memory has been claimed during the constructor, you're on your own to have it freed before leaving the constructor with the exception.

                  For your question WHO deletes the memory the answer is the code behind the new-operator (which is generated by the compiler). If it recognizes an exception leaving the constructor it has to call all the destructors of the classes members (as those have already been constructed successfully prior calling the constructor code) and free their memory (could be done recursively together with destructor-calling, most probably by calling a proper delete on them) as well as free the memory allocated for this class itself. Then it has to rethrow the catched exception from the constructor to the caller of new. Of course there may be more work which has to be done but I cannot pull out all the details from my head because they are up to each compiler's implementation.

                  這篇關于誰刪除了在“新建"期間分配的內存?構造函數中有異常的操作?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  In what ways do C++ exceptions slow down code when there are no exceptions thown?(當沒有異常時,C++ 異常會以何種方式減慢代碼速度?)
                  Why catch an exception as reference-to-const?(為什么要捕獲異常作為對 const 的引用?)
                  When and how should I use exception handling?(我應該何時以及如何使用異常處理?)
                  Scope of exception object in C++(C++中異常對象的范圍)
                  Catching exceptions from a constructor#39;s initializer list(從構造函數的初始化列表中捕獲異常)
                  Difference between C++03 throw() specifier C++11 noexcept(C++03 throw() 說明符 C++11 noexcept 之間的區別)
                  <tfoot id='sK7Du'></tfoot>

                      • <bdo id='sK7Du'></bdo><ul id='sK7Du'></ul>
                        <legend id='sK7Du'><style id='sK7Du'><dir id='sK7Du'><q id='sK7Du'></q></dir></style></legend>

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

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

                          1. 主站蜘蛛池模板: 福利片在线 | 免费黄色一级视频 | 男女啪啪网站 | 久久都是精品 | 韩日av在线| 国产伦精品一区二区三区视频黑人 | 操出白浆视频 | 福利小视频在线观看 | 精品亚洲一区二区三区四区五区 | 伊人国产女 | 中文字幕在线观看一区二区三区 | 亚洲天堂网在线观看 | 精品自拍视频 | 欧美一级淫片免费视频黄 | 中文字幕有码在线 | 久久久久黄色 | 色婷婷精品国产一区二区三区 | 日韩av在线影院 | 日韩精品三区 | 青草国产 | 日韩中文视频 | 欧美激情网址 | 欧美又大又硬又粗bbbbb | 黄色小视频免费看 | 我想看毛片 | 免费高清av | 一区二区三区四区国产 | 国产精品成人国产乱一区 | 精品视频在线免费观看 | 日韩精品免费一区二区夜夜嗨 | 五月开心网| 欧美视频一区 | 天天干天天操天天爽 | 国产成人免费在线观看 | 一本一道久久a久久精品蜜桃 | 黄视频在线播放 | 日日操日日干 | 精品网站999www| 黄色影音 | 亚洲天堂欧美 | 五月婷婷丁香综合 |