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

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

Related (duplicate?): In what cases do I use malloc vs new?


new/delete

  • Allocate/release memory
  • Memory allocated from 'Free Store'
  • Returns a fully typed pointer.
  • new (standard version) never returns a NULL (will throw on failure)
  • Are called with Type-ID (compiler calculates the size)
  • Has a version explicitly to handle arrays.
  • Reallocating (to get more space) not handled intuitively (because of copy constructor).
  • Whether they call malloc/free is implementation defined.
  • Can add a new memory allocator to deal with low memory (set_new_handler)
  • operator new/delete can be overridden legally
  • constructor/destructor used to initialize/destroy the object
  • malloc/free

  • Allocates/release memory
  • Memory allocated from 'Heap'
  • Returns a void*
  • Returns NULL on failure
  • Must specify the size required in bytes.
  • Allocating array requires manual calculation of space.
  • Reallocating larger chunk of memory simple (No copy constructor to worry about)
  • They will NOT call new/delete
  • No way to splice user code into the allocation sequence to help with low memory.
  • malloc/free can NOT be overridden legally
  • Table comparison of the features:

     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                            
    

    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调用析构函数然后释放内存。


    new calls the ctor of the object, delete call the dtor.

    malloc & free just allocate and release raw memory.

    链接地址: http://www.djcxy.com/p/240.html

    上一篇: Stack,Static和Heap in C ++

    下一篇: new / delete和malloc / free有什么区别?