What is the difference between delete and free?
Answer:
irst, let see what is the ‘delete’ and ‘free’ in C++, then we will see the difference between them.
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
A free function is used to deallocate memory allocated by the malloc() or calloc() function.
The general syntax to use free:
free(ptr);
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
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. 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