久久久久久久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. 主站蜘蛛池模板: 中文字幕日韩欧美一区二区三区 | ww亚洲ww亚在线观看 | 国产成人99久久亚洲综合精品 | 夜夜撸av| 日韩a在线 | 亚洲综合二区 | 午夜在线| 日本在线视频一区二区 | 亚洲精品2区 | 亚洲成网站 | 久久精品国产99国产精品亚洲 | 免费国产视频 | 99久热在线精品视频观看 | 中文字幕国产精品视频 | 亚洲天堂av网 | 亚洲一区二区精品视频 | 日本精品一区二区三区视频 | 午夜精品影院 | 国产午夜亚洲精品不卡 | 国产精品区二区三区日本 | 国产一区二区在线视频 | 青青久久av北条麻妃海外网 | 黄色一级视频 | 国产精品伦一区二区三级视频 | 国产精品二区三区在线观看 | 天堂成人国产精品一区 | 亚洲视频欧美视频 | 国产精品久久久久无码av | 亚洲国产日本 | 亚洲欧美在线观看 | 日韩成人久久 | 五月天国产在线 | 午夜不卡福利视频 | 91在线电影 | 欧美精品一二三区 | 黄色毛片免费 | 久久久久久国产一区二区三区 | 日韩成人中文字幕 | 自拍偷拍亚洲欧美 | 国产精品久久久久久久免费大片 | 国产精品亚洲片在线播放 |