Environmental Sciences, asked by Mohit0, 1 year ago

Difference between malloc() and operator new.

Answers

Answered by ananthblaze
0

The most relevant difference is that the new operator allocates memory then calls the constructor, and delete calls the destructor then deallocates the memory. new calls the ctor of the object, delete call the dtor. malloc & free just allocate and release raw memory.

Answered by saurav01234567890
0
Following are the differences between malloc() and operator new.

new is an operator, while malloc() is a function.
new returns exact data type, while malloc() returns void *.
new calls constructors( class instances are initalized and deinitialized automatically), while malloc() does not( classes won’t get initalized or deinitialized automatically
Syntax:
int *n = new int(10); // initialization with new()
str = (char *) malloc(15); //malloc()
free( ) is used on resources allocated by malloc( ), or calloc( ) in C



Delete is used on resources allocated by new in C+
Similar questions