pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <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="3h5km30" 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="n8hkvmw" 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="zvhp5rd" id='sK7Du'><tfoot id='sK7Du'></tfoot><dl id='sK7Du'><fieldset id='sK7Du'></fieldset></dl></div>

                          1. 主站蜘蛛池模板: 电动百叶窗,开窗器,电动遮阳百叶,电动开窗机生产厂家-徐州鑫友工控科技发展有限公司 | 呼末二氧化碳|ETCO2模块采样管_气体干燥管_气体过滤器-湖南纳雄医疗器械有限公司 | 耳模扫描仪-定制耳机设计软件-DLP打印机-asiga打印机-fitshape「飞特西普」 | 济南网站建设|济南建网站|济南网站建设公司【济南腾飞网络】【荐】 | 一体化污水处理设备,一体化污水设备厂家-宜兴市福源水处理设备有限公司 | 防爆电机_ybx3系列电机_河南省南洋防爆电机有限公司 | 大倾角皮带机-皮带输送机-螺旋输送机-矿用皮带输送机价格厂家-河南坤威机械 | 火锅加盟_四川成都火锅店加盟_中国火锅连锁品牌十强_朝天门火锅【官网】 | 杭州高温泵_热水泵_高温油泵|昆山奥兰克泵业制造有限公司 | 医用空气消毒机-医用管路消毒机-工作服消毒柜-成都三康王 | 金刚网,金刚网窗纱,不锈钢网,金刚网厂家- 河北萨邦丝网制品有限公司 | 耐火砖厂家,异形耐火砖-山东瑞耐耐火材料厂 | 智能电表|预付费ic卡水电表|nb智能无线远传载波电表-福建百悦信息科技有限公司 | 自动记录数据电子台秤,记忆储存重量电子桌称,设定时间记录电子秤-昆山巨天 | 开云(中国)Kaiyun·官方网站-登录入口 | 耐酸碱胶管_耐腐蚀软管总成_化学品输送软管_漯河利通液压科技耐油耐磨喷砂软管|耐腐蚀化学软管 | 跨境物流_美国卡派_中大件运输_尾程派送_海外仓一件代发 - 广州环至美供应链平台 | 智能电表|预付费ic卡水电表|nb智能无线远传载波电表-福建百悦信息科技有限公司 | 石家庄律师_石家庄刑事辩护律师_石家庄取保候审-河北万垚律师事务所 | 比亚迪叉车-比亚迪电动叉车堆垛车托盘车仓储叉车价格多少钱报价 磁力去毛刺机_去毛刺磁力抛光机_磁力光饰机_磁力滚抛机_精密金属零件去毛刺机厂家-冠古科技 | 电表箱-浙江迈峰电力设备有限公司-电表箱专业制造商 | 塑胶跑道施工-硅pu篮球场施工-塑胶网球场建造-丙烯酸球场材料厂家-奥茵 | 合肥宠物店装修_合肥宠物美容院装修_合肥宠物医院设计装修公司-安徽盛世和居装饰 | 【中联邦】增稠剂_增稠粉_水性增稠剂_涂料增稠剂_工业增稠剂生产厂家 | 氮化镓芯片-碳化硅二极管 - 华燊泰半导体 | 苏州西装定制-西服定制厂家-职业装定制厂家-尺品服饰西装定做公司 | 协议书_协议合同格式模板范本大全 | TwistDx恒温扩增-RAA等温-Jackson抗体-默瑞(上海)生物科技有限公司 | 托盘租赁_塑料托盘租赁_托盘出租_栈板出租_青岛托盘租赁-优胜必达 | 南京欧陆电气股份有限公司-风力发电机官网 | 深圳市八百通智能技术有限公司官方网站 | 三价铬_环保铬_环保电镀_东莞共盈新材料贸易有限公司 | 海外仓系统|国际货代系统|退货换标系统|WMS仓储系统|海豚云 | 扒渣机,铁水扒渣机,钢水扒渣机,铁水捞渣机,钢水捞渣机-烟台盛利达工程技术有限公司 | 压滤机滤板_厢式_隔膜_板框压滤机滤板厂家价格型号材质-大凯环保 | 杭州月嫂技术培训服务公司-催乳师培训中心报名费用-产后康复师培训机构-杭州优贝姆健康管理有限公司 | 圣才学习网-考研考证学习平台,提供万种考研考证电子书、题库、视频课程等考试资料 | 山东信蓝建设有限公司官网 | 岩棉切条机厂家_玻璃棉裁条机_水泥基保温板设备-廊坊鹏恒机械 | 高铝砖-高铝耐火球-高铝耐火砖生产厂家-价格【荣盛耐材】 | 讲师宝经纪-专业培训机构师资供应商_培训机构找讲师、培训师、讲师经纪就上讲师宝经纪 |