+1 vote
in C Plus Plus by

1 Answer

0 votes
by

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:
    1. int *n = new int(10); // initialization with new()
    2. 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++

Related questions

+1 vote
+1 vote
asked Jul 16, 2019 in C Plus Plus by Indian
+1 vote
+1 vote
asked Jul 16, 2019 in C Plus Plus by Indian
...