Q:

What is the difference between delete and free in c++?

0

What is the difference between delete and free?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

irst, let see what is the ‘delete’ and ‘free’ in C++, then we will see the difference between them.

Delete:

Delete is an Operator in C++ which is used to free the memory allocated by the ‘new’ operator. It is also called the destructor of the class.

The following is the general syntax of delete expression.

1. ::opt delete cast-expression
2. ::opt delete [ ] cast-expression

1. Destroys one non-array object created by a new-expression.

2. Destroys an array created by a new[]-expression

Free():

A free function is used to deallocate memory allocated by the malloc() or calloc() function.

The general syntax to use free:

free(ptr);

Some differences between delete and free:

  • ‘delete’ is an operator while ‘free’ is a function.
  • ‘delete’ frees the allocated memory which allocates by new and free frees the memory allocated by malloc, calloc, realloc.
  • ‘delete’ calls the destructor while free does not call any destructor.
  • free() uses C run time heap whereas delete may be overloaded on a class basis to use private heap.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions and Answers(2022)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What do you mean by call by value and call by refe... >>
<< What is the difference between new and malloc in c...