Friday, November 7, 2008

new vs malloc


new vs malloc

new
1.new allocates memory and intializes it by calling constructor
2.new returns the pointer to the type which it has just allocated memory,no need to cast
for example: new int ---- This returns a integer pointer
3.new makes sure to allocate memory successfully before calling constructor so no need check whether the call is successfull or not
4.new is an operator.
malloc
1.malloc does only memory allocation,not intialization
2.malloc returns a void pointer,you have to cast it.
3.If malloc fails to allocate memory it returns a NULL pointer.so u must check the returned pointer.
4.malloc is a function

0 comments:

Hit Counter


Statistics

Followers

 

Programmer's Guide. Copyright 2008 All Rights Reserved