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

前端 未结 15 2721
春和景丽
春和景丽 2020-11-21 23:53

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

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

相关标签:
15条回答
  • 2020-11-22 00:02

    The most relevant difference is that the new operator allocates memory then calls the constructor, and delete calls the destructor then deallocates the memory.

    0 讨论(0)
  • 2020-11-22 00:03

    The main difference between new and malloc is that new invokes the object's constructor and the corresponding call to delete invokes the object's destructor.

    There are other differences:

    • new is type-safe, malloc returns objects of type void*

    • new throws an exception on error, malloc returns NULL and sets errno

    • new is an operator and can be overloaded, malloc is a function and cannot be overloaded

    • new[], which allocates arrays, is more intuitive and type-safe than malloc

    • malloc-derived allocations can be resized via realloc, new-derived allocations cannot be resized

    • malloc can allocate an N-byte chunk of memory, new must be asked to allocate an array of, say, char types

    Looking at the differences, a summary is malloc is C-esque, new is C++-esque. Use the one that feels right for your code base.

    Although it is legal for new and malloc to be implemented using different memory allocation algorithms, on most systems new is internally implemented using malloc, yielding no system-level difference.

    0 讨论(0)
  • 2020-11-22 00:03

    also,

    the global new and delete can be overridden, malloc/free cannot.

    further more new and delete can be overridden per type.

    0 讨论(0)
  • 2020-11-22 00:03
    • To use the malloc(), we need to include <stdlib.h> or <alloc.h> in the program which is not required for new.
    • new and delete can be overloaded but malloc can not.
    • Using the placement new, we can pass the address where we want to allocate memory but this is not possible in case of malloc.
    0 讨论(0)
  • 2020-11-22 00:04

    The only similarities are that malloc/new both return a pointer which addresses some memory on the heap, and they both guarantee that once such a block of memory has been returned, it won't be returned again unless you free/delete it first. That is, they both "allocate" memory.

    However, new/delete perform arbitrary other work in addition, via constructors, destructors and operator overloading. malloc/free only ever allocate and free memory.

    In fact, new is sufficiently customisable that it doesn't necessarily return memory from the heap, or even allocate memory at all. However the default new does.

    0 讨论(0)
  • 2020-11-22 00:07

    new and delete are operators in c++; which can be overloaded too. malloc and free are function in c;

    malloc returns null ptr when fails while new throws exception.

    address returned by malloc need to by type casted again as it returns the (void*)malloc(size) New return the typed pointer.

    0 讨论(0)
提交回复
热议问题