Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   What is the difference between malloc and new other than syntax? (http://www.go4expert.com/forums/difference-malloc-syntax-t10644/)

aspguy 16May2008 17:45

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

shabbir 16May2008 21:54

Re: 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

atul.sharma12 24May2008 11:56

Re: What is the difference between malloc and new other than syntax?
 
One more Difference..
Memory allocated using Malloc can be resized using realloc.But It,s not possible in case of new.


All times are GMT +5.5. The time now is 17:41.