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

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

问题描述

/ 删除 malloc之间有什么区别 / free

相关(重复?):在什么情况下使用malloc vs new?

推荐答案

新建/删除




  • 分配/释放内存

    new/delete

    • Allocate/release memory


      1. 从Free Store分配的内存

      2. 返回完全类型化的指针。

      3. )不会返回NULL(会抛出失败)

      4. 用Type-ID调用(编译器计算大小)


      5. 重新分配(以获得更多空间)不直观地处理(因为复制构造函数)。


      6. 可以添加新的内存分配器来处理低内存(set_new_handler)

      7. 可以合法覆盖operator new / delete

      8. 用于初始化/销毁对象的构造函数/析构函数

      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 (set_new_handler)
      9. operator new/delete can be overridden legally
      10. constructor/destructor used to initialize/destroy the object



      • 分配/释放内存

      • Allocates/release memory

      1. 从'Heap'分配的内存

      2. 返回void *

      3. 失败时返回NULL

      4. 必须指定所需的字节大小。

      5. 分配数组需要手动计算空间。

      6. 重新分配较大的内存块构造函数担心)

      7. 他们将调用新的/删除

      8. 没有办法将用户代码拼接到分配

      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


      b $ b

      功能的表比较:

       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 (con-)/destructor | Yes                            | No                            
      

      技术上,new分配的内存来自'Free Store',而malloc分配的内存来自'堆'。这两个区域是否相同是一个实现细节,这是malloc和new不能混合的另一个原因。

      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 details, which is another reason that malloc and new can not be mixed.

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

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