Jul 16, 2019 in C Plus Plus
Q:

Malloc() vs new / Delete vs Free ?

1 Answer

0 votes
Jul 16, 2019

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

Click here to read more about Loan/Mortgage
Click here to read more about Insurance

Related questions

+1 vote
Jul 16, 2019 in C Plus Plus
...