What is the use of new and delete operators? Illustrate with example. What are advantages of new malloc.

This answer is restricted. Please login to view the answer of this question.

Login Now

The new operator denotes a request for memory allocation on the Free Store. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable. 

Synatx:

pointer-variable = new data-type;

Once we no longer need to use a variable that we have declared dynamically, we can deallocate the memory occupied by the variable.

For this, the delete operator is used. It returns the memory to the operating system. This is known as memory deallocation.

Synatx:

delete pointer-variable;

Advantage of new over malloc are

  • new does not need the sizeof() operator where as malloc() needs to know the size before memory allocation.
  • Operator new can make a call to a constructor where as malloc() cannot.
  • new can be overloaded malloc() can never be overloaded.
  • new could initialize object while allocating memory to it where as malloc () cannot.
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .