- Back to Home »
- Differences between new/delete and malloc/free
Posted by : Sushanth
Monday, 14 December 2015
Differences between new/delete and malloc/free:
When new has an object, space for the object is not only allocated but the object's constructor is called. And similarly when delete as an object, the object's destructor is called before the memory is released. If malloc and free are used, the destructor and constructor do not get called respectively and obviously, this simply won't do in C++ except in certain very rare situations where classes are present without any specific destructor/constructors.
- 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).
- If 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
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.