Difference Between new/delete vs malloc/free


FREE FUNCTION:

The main job of the free function is to free up or release the memory which is allocated at the runtime. The free() function can be used in both C as well as C++ programming language.

DELETE:

The delete operator also has the same job like free function, that is, to release the memory which is allotted at the runtime. The delete operator is most commonly used in C++ programming.

DIFFERENCE BETWEEN FREE AND DELETE:

  1. The free function is basically a library function which resides in the stdlib.h header file, while delete, on the other hand, is an operator which is most commonly used in C++ programming.
  1. In the free() function, there is no call made to the destructor after the release of the runtime allocated memory. On the other hand, in case of delete operator, there is a call made to the destructor immediately after the release of the allocated memory.
  1. In case of the free() function, as we all know that this is a function and thus it requires to get its declaration from the header file which makes it slower. In case of delete operator, as it is an operator and we all know that an operator is faster paced than a function.

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.

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.

DIFFERENCE BETWEEN MALLOC AND NEW:

  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.
  1. 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.
  2. 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.


Posted in Articles and tagged , , , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *