Get the FREE certificate on C, C++, Java, Python, Perl, PHP, SQL, Linux & Javascript!
Home C C++ Java Python Perl PHP Spring SQL Javascript Linux

You are here : Home » Difference between new & malloc

What is the difference between C++ new and C malloc

C++ NewC Malloc
  • 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
  • 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