| |
|
both malloc and new functions are used for dynamic memory allocations and the
basic difference is: malloc requires a special "typecasting" when it allocates
memory for eg. if the pointer used is the char pointer then after the processor
allocates memory then this allocated memory needs to be typecasted to char
pointer i.e (char*).but new does not requires any typecasting. Also, free is
the keyword used to free the memory while using malloc and delete the keyword
to free memory while using new, otherwise this will lead the memory leak.
Answer2:
new automatically calls the constructor while malloc() dosen't., also new being
an operator, it can be overloaded. since malloc returns a void pointer it is
necessary to explicitly typecast it into an appropriate type of pointer. This
gets completely avoided when we are using new operator
|
|