Difference Between New and Malloc in C++

MALLOC FUNCTION:

The main job of the malloc function is to gear up and allow the memory at the runtime. The malloc() function can be used in both C as well as C++ programming language.

ptr = (type*) malloc(size)

NEW:

The new operator has also the same job like malloc function, that is, to allot the memory at the runtime. The new operator is most commonly used in C++ programming.

type variable = new dataType()

 

DIFFERENCE BETWEEN NEW AND MALLOC IN C++ :

Basismallocnew
Refers toA Library functionAn operator
Returnsvoid*fully typed pointer
OverridingNot PossiblePossible
Execution speedslowfast
  1. The malloc function is basically a library function which resides in the stdlib.h header file.The new on the other hand is an operator which is most commonly used in C++ programming.
  2. In the case of malloc function, there is no constructor call made to allocate the memory at the runtime. In case of new operator, there is a constructor call made to the memory at the runtime.
  1. In case of malloc function, the allotment of the memory at runtime is done from the heap. In case of new operator, the allotment of the memory at the runtime is done from the free store.

Leave a Reply