2008年6月16日星期一

malloc free vs new delete

[malloc free vs new delete]
Advantages of new over malloc
1. new invokes the constructor of the object created. malloc does not.
2. new does not need an explicit typecasting as in the case of malloc
[int *p=(int *)malloc(sizeof(int));]
3. new can be overloaded. malloc cannot be.
4. new does not need you to specify the size of the object being allocated.
[C * c = new C;]

Advantages of delete over malloc
1. delete can be overloaded. free cannot.
2. delete invokes the destructor of the object to be deallocated. free
does not do this.

0 意見: