What is the difference between new/delete and malloc/free?
Related (duplicate?): In what cases do I use malloc vs new?
What is the difference between new/delete and malloc/free?
Related (duplicate?): In what cases do I use malloc vs new?
The most relevant difference is that the new operator allocates memory then calls the constructor, and delete calls the destructor then deallocates the memory.
new
calls the ctor of the object, delete
call the dtor.
malloc
& free
just allocate and release raw memory.
In C++ New/Delete call the Constructor/Destructor accordingly.
Malloc/Free simply allocate memory from the heap. New/Delete allocate memory as well.
new/delete is C++, malloc/free comes from good old C.
In C++, new calls an objects constructor and delete calls the destructor.
malloc and free, coming from the dark ages before OO, only allocate and free the memory, without executing any code of the object.
New and delete are C++ primitives which declare a new instance of a class or deletes them (thus invoking the destructor of the class for the instance).
malloc and free are C primitives and they allocate and free memory blocks (in size).
Both use the heap to make the allocation. Malloc and free are nontheless 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).
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.
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.
also,
the global new and delete can be overridden, malloc/free cannot.
further more new and delete can be overridden per type.