While searching I found a very naive, but useful thing about new/malloc.
Why should we use new instead of malloc ?
Why should we use new instead of malloc ?
- New and Delete makes sure that constructors and sestructors are called but not the case with malloc and free functions of C.
- Pointer conversion safety: malloc() returns a void* which isn't safe. new returns a pointer of the right type.
- new is a Operator: new is an operator that can be overloaded for better memory management by a class, while malloc() is not an operaotor.
- new operator computes the size of object automatically whereas malloc cannot.
- It is possible to initialize the object while allocating memory with new.
- Its possible to construct an object over an already allocated memory using another version of 'new' operator known as 'placement new' operator. While with malloc() it is not possible.