1.

What is the difference between new() and malloc()?

Answer»

In C++, both malloc() and new are used for memory ALLOCATION at runtime. The differences are listed below:

  • Malloc() is a function, whereas new() is a pre-processor.
  • When using new(), there is no requirement to allocate memory; nevertheless, when using malloc(), you must use sizeof ().
  • new() SETS the new memory to 0 and malloc() assigns a RANDOM value to the newly allocated memory.
  • The new() OPERATOR ALLOCATES memory and invokes the constructor for object initialization, whereas the malloc() function allocates memory but does not invoke the constructor.
  • Because the operator is faster than the function, the new() operator is faster than the malloc() function.


Discussion

No Comment Found