<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C++核心準(zhǔn)則:R.10: 避免使用macloc()和free()

          43ad77930dba6394246661acdb082bfd.webp

          R.10: Avoid?malloc()?and?free()

          R.10: 避免使用macloc()和free()


          Reason(原因)

          malloc()?and?free()?do not support construction and destruction, and do not mix well with?new?and?delete.

          malloc()和free()不支持構(gòu)造和析構(gòu),和new/delete融合得也不好。


          Example(示例)

          class Record {
          int id;
          string name;
          // ...
          };

          void use()
          {
          // p1 may be nullptr
          // *p1 is not initialized; in particular,
          // that string isn't a string, but a string-sized bag of bits
          Record* p1 = static_cast(malloc(sizeof(Record)));

          auto p2 = new Record;

          // unless an exception is thrown, *p2 is default initialized
          auto p3 = new(nothrow) Record;
          // p3 may be nullptr; if not, *p3 is default initialized

          // ...

          delete p1; // error: cannot delete object allocated by malloc()
          free(p2); // error: cannot free() object allocated by new
          }

          In some implementations that?delete?and that?free()?might work, or maybe they will cause run-time errors.

          在某些實(shí)現(xiàn)的的情況下,這里delete和free()可能可以執(zhí)行,也可能引起執(zhí)行時(shí)錯誤。


          delete釋放malloc申請的內(nèi)存,而free釋放的是new構(gòu)建的對象。

          ----譯者注


          Exception(例外)

          There are applications and sections of code where exceptions are not acceptable. Some of the best such examples are in life-critical hard-real-time code. Beware that many bans on exception use are based on superstition (bad) or by concerns for older code bases with unsystematic resource management (unfortunately, but sometimes necessary). In such cases, consider the?nothrow?versions of?new.

          有些應(yīng)用或者代碼片段不能接受異常。這方面最好的例子是生命周期敏感的硬實(shí)時(shí)代碼。注意很多關(guān)于異常的禁令都是基于(不好的)迷信或者對沒有系統(tǒng)進(jìn)行資源管理的舊代碼的擔(dān)憂(雖然很不幸,但有時(shí)是必要的)。這種情況下,考慮不拋出異常的new。


          Enforcement(實(shí)施建議)

          Flag explicit use of?malloc?and?free.

          標(biāo)識出顯式使用malloc和free的情況。


          原文鏈接:

          https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r10-avoid-malloc-and-free




          覺得本文有幫助?請分享給更多人。

          關(guān)注【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!

          面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>

          瀏覽 52
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产精品探花熟女AV | 老熟女朝逼 | 欧美三级电影中文字幕 | 玖玖成人免费 | 五月天就去色色 |