new/delete 和 malloc/free 有什么区别? [英] What is the difference between new/delete and malloc/free?

查看:35
本文介绍了new/delete 和 malloc/free 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

new/deletemalloc/free 有什么区别?

What is the difference between new/delete and malloc/free?

相关(重复?):在什么情况下我应该使用 malloc 还是 new?

推荐答案

new/delete

  • 分配/释放内存

    new / delete

    • Allocate / release memory

      1. 从免费存储"分配的内存.
      2. 返回一个完全类型的指针.
      3. new(标准版本)从不返回 NULL(失败时会抛出).
      4. 使用类型 ID 调用(编译器计算大小).
      5. 有一个明确处理数组的版本.
      6. 重新分配(以获得更多空间)未直观处理(因为复制构造函数).
      7. 是否调用 malloc/free 是实现定义的.
      8. 可以添加一个新的内存分配器来处理低内存(std::set_new_handler).
      9. operator new/operator delete 可以合法覆盖.
      10. 用于初始化/销毁对象的构造函数/析构函数.
      1. Memory allocated from 'Free Store'.
      2. Returns a fully typed pointer.
      3. new (standard version) never returns a NULL (will throw on failure).
      4. Are called with Type-ID (compiler calculates the size).
      5. Has a version explicitly to handle arrays.
      6. Reallocating (to get more space) not handled intuitively (because of copy constructor).
      7. Whether they call malloc / free is implementation defined.
      8. Can add a new memory allocator to deal with low memory (std::set_new_handler).
      9. operator new / operator delete can be overridden legally.
      10. Constructor / destructor used to initialize / destroy the object.

      • 分配/释放内存
      • Allocate / release memory
      1. 从堆"分配的内存.
      2. 返回一个 void*.
      3. 失败时返回 NULL.
      4. 必须以字节为单位指定所需的大小.
      5. 分配数组需要手动计算空间.
      6. 重新分配更大的内存块很简单(无需担心复制构造函数).
      7. 他们不会调用new/delete.
      8. 无法将用户代码拼接到分配序列中以帮助降低内存.
      9. malloc/free 可以不能被合法地覆盖.
      1. Memory allocated from 'Heap'.
      2. Returns a void*.
      3. Returns NULL on failure.
      4. Must specify the size required in bytes.
      5. Allocating array requires manual calculation of space.
      6. Reallocating larger chunk of memory simple (no copy constructor to worry about).
      7. They will NOT call new / delete.
      8. No way to splice user code into the allocation sequence to help with low memory.
      9. malloc / free can NOT be overridden legally.

      <头>
      功能新建/删除malloc/free
      内存分配自'免费商店''堆'
      退货全类型指针void*
      失败时抛出(从不返回NULL)返回NULL
      所需尺寸编译器计算必须以字节为单位指定
      处理数组有明确的版本需要手动计算
      重新分配不直观简单(无复制构造函数)
      反向调用实现定义没有
      低内存情况可以添加一个新的内存分配器不被用户代码处理
      可覆盖没有
      构造函数/析构函数的使用没有
      Feature new / delete malloc / free
      Memory allocated from 'Free Store' 'Heap'
      Returns Fully typed pointer void*
      On failure Throws (never returns NULL) Returns NULL
      Required size Calculated by compiler Must be specified in bytes
      Handling arrays Has an explicit version Requires manual calculations
      Reallocating Not handled intuitively Simple (no copy constructor)
      Call of reverse Implementation defined No
      Low memory cases Can add a new memory allocator Not handled by user code
      Overridable Yes No
      Use of constructor / destructor Yes No

      从技术上讲,new 分配的内存来自Free Store",而malloc 分配的内存来自Heap".这两个区域是否相同是一个实现细节,这也是mallocnew不能混用的另一个原因.

      Technically, memory allocated by new comes from the 'Free Store' while memory allocated by malloc comes from the 'Heap'. Whether these two areas are the same is an implementation detail, which is another reason that malloc and new cannot be mixed.

      这篇关于new/delete 和 malloc/free 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆