1.

Differentiate between new and malloc() in the context of C++.

Answer»

The following table lists the differences between new and malloc():

newmalloc()
new is an operator.malloc() is a function.
When we use a new operator, it calls the constructor.Usage of the malloc() function does not involve invoking constructors.
It RETURNS the DATA TYPE specified while using the new operator. It returns a VOID * type which needs to be typecasted manually to the required data type.
When the new operator fails, it THROWS an error of bad_alloc exception.When the malloc() command fails, it returns NULL.
Here, the size of memory required is computed automatically by the compiler.Here, the size of memory required needs to be computed manually and provided at the time of using malloc().


Discussion

No Comment Found