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++