What is the difference between malloc and new other than syntax?

aspguy's Avatar, Join Date: May 2005
Contributor
What is the difference between malloc and new other than syntax?
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
The main differences are as follows

1. Operator new constructs an object (calls constructor of object), malloc does not.

2. Operator new is an operator and so it can be overloaded, where as malloc is a function.

3. Implementation is different like new throws an exception if there is not enough memory, malloc returns a NULL.

4. Operator new[] requires to specify the number of objects to allocate, malloc requires to specify the total number of bytes to allocate.

5. malloc() returns void *, which has to be explicitly cast to the desired type but new returns the proper type.

Apart from new / malloc in C++ there is no operator for realloc
0
atul.sharma12's Avatar, Join Date: May 2008
Go4Expert Member
One more Difference..
Memory allocated using Malloc can be resized using realloc.But It,s not possible in case of new.