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

前端 未结 15 2929
春和景丽
春和景丽 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:14

    new and delete are C++ primitives which declare a new instance of a class or delete it (thus invoking the destructor of the class for the instance).

    malloc and free are C functions and they allocate and free memory blocks (in size).

    Both use the heap to make the allocation. malloc and free are nonetheless more "low level" as they just reserve a chunk of memory space which will probably be associated with a pointer. No structures are created around that memory (unless you consider a C array to be a structure).

提交回复
热议问题