Discussion in 'C' started by aspguy, May 16, 2008.
What is the difference between malloc and new other than syntax?
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
One more Difference..
Memory allocated using Malloc can be resized using realloc.But It,s not possible in case of new.
Separate names with a comma.