In C++, both the free function and the delete operator are mainly utilized to deallocate dynamically allocated memory. Both have similar functionality, but they are different in several contexts, such as origin, usage, behavior, type-safety, and many others.
In this article, we will discuss the difference between the free function and the delete operator in C++. Before discussing their differences, we must know about the free and delete in C++.
What is the free function?
In C++ , the free function is used to deallocate the memory dynamically. It is basically a library function that is mainly used in C++, and it is defined in stdlib.h header file. This library function is used when the pointers either point to the memory allocated using the malloc function or Null pointer.
Syntax
It has the following syntax:
free(ptr_name);
In this syntax,
- ptr_name: It represents the name of the pointer.
- Return Value: The free function does not return any value. Its main function is to free the memory.
C++ Free Function Example with Malloc
Let us take an example to illustrate the free function with malloc in C++.
Example
#include <iostream>
#include <cstdlib>
using namespace std; //using standard namespace
int main() //main function
{
int *ptr;
ptr = (int*) malloc(5*sizeof(int));
cout << "Enter 5 integer" << endl;
for (int i=0; i<5; i++)
{
// *(ptr+i) can be replaced by ptr[i]
cin >>ptr[i];
}
cout << endl << "User entered value"<< endl;
for (int i=0; i<5; i++)
{
cout <<*(ptr+i) << " ";
}
free(ptr);
/* prints a garbage value after ptr is free */
cout << "Garbage Value" << endl;
for (int i=0; i<5; i++)
{
cout << *(ptr+i)<< " ";
}
return 0;
}
Output:
Enter 5 integer
4
6
2
5
7
User entered value
4 6 2 5 7 Garbage Value
286744300 6 -753986992 -288116722 7
Explanation:
In this example, we demonstrate the free function works with the malloc function. First, we declare integer pointer *ptr, and then we allocate the memory to this pointer variable by using the malloc function. After that, ptr is pointing to the uninitialized memory block of 5 integers. After allocating the memory, we use the free function to destroy this allocated memory. When we try to print the value, which is pointed by the ptr, we get a garbage value, which means that memory is deallocated.
C++ Free Function Example with Calloc
Let us take an example to illustrate the free function with Calloc in C++.
Example
#include <iostream>
#include <cstdlib>
using namespace std; //using standard namespace
int main() //main function
{
float *ptr; // floacpp tutorialer declaration
ptr=(float*)calloc(1,sizeof(float));
*ptr=6.7;
cout << "The value of *ptr before applying the free() function : " <<*ptr<< endl;
free(ptr);
cout << "The value of *ptr after applying the free() function : " <<*ptr<< endl;
return 0;
}
Output:
The value of *ptr before applying the free() function : 6.7
The value of *ptr after applying the free() function : 8.13715e-37
Explanation:
In this example, we can observe that the free function works with a calloc function. We use the calloc function to allocate the memory block to the floacpp tutorialer ptr. After that, we have assigned a memory block to the ptr that can have a single float type value.
What is the Delete Operator?
The delete is an operator that is used in C++ programming language , and it is used to deallocate the memory dynamically. This operator is mainly used for those pointers that are allocated using a new operator or NULL pointer.
Syntax
It has the following syntax:
delete pointer_name
If we want to delete the allocated memory block, we can use the following syntax:
delete p;
To delete the array, we use the statement as given below:
delete [] p;
Important Points about Delete Operator
Some importancpp tutorials related to the delete operator are as follows:
- It is either used to delete the array or non-array objects, which are allocated by using the new keyword.
- We use the delete and delete operator to delete the array or non-array object.
- The new keyword allocated the memory in a heap; therefore, we can say that the delete operator always deallocates the memory from the heap.
- It does not destroy the pointer, but the value or the memory block, which is pointed by the pointer is destroyed.
C++ Delete Operator Example
Let us take an example to illustrate the delete operator in C++.
Example
#include <iostream>
#include <cstdlib>
using namespace std; //using standard namespace
int main() //main function
{
int *ptr;
ptr=new int;
*ptr=68;
cout << "The value of p is : " <<*ptr<< endl;
delete ptr;
cout <<"The value after delete is : " <<*ptr<< endl;
return 0;
}
Output:
The value of p is : 68
The value after delete is : -1692301068
Explanation:
In this example, we use the new operator to allocate the memory, so we use the delete ptr operator to destroy the memory block, which is pointed by the pointer ptr.
Main differences between free and Delete
There are several differences between delete and free in C++. Some main differences between free and delete are as follows:
- The delete is an operator that deallocates the memory dynamically. On the other hand, the free is a function that destroys the memory at the runtime.
- The delete operator is used to delete the pointer, which is either allocated using the new operator or a NULL pointer. On the other hand, the free function is used to delete the pointer that is either allocated using malloc, calloc, and realloc function or NULL pointer.
- When the delete operator destroys the allocated memory, it calls the destructor of the class in C++. On the other hand, the free function does not call the destructor; it only frees the memory from the heap.
- The delete operator is faster than the free function.
- The delete operator releases the allocated memory and calls the destructor. On the other hand, the free function deallocate the memory, but it does not call the destructor.
Difference between free and delete Operator in Tabular Form
There are several differences between delete and free in C++. Some main differences between free and delete in Tabular Form are as follows:
| Features | Free | Delete |
|---|---|---|
Type |
It is a library function. | It is an operator. |
| Memory Deallocation | It should only be utilized to deallocate the memory allocated either using malloc(), calloc(), realloc() or for a NULL pointer. | It should only be utilized to deallocate the memory allocated either using the new operator or for a NULL pointer. |
| Destructor | It doesn't call destructor. | It calls the destructor. |
| Overloaded | It cannot be overloaded. | It can be overloaded. |
| Type-Safety | It has no type-safety. | It provides type-safety. |
| Speed | It is faster than the delete operator. | It is slower than the free operator because it calls the destructor for the object that is deleted before deallocating the memory. |
| Memory | It destroys the memory at the runtime. | It deallocates the memory dynamically. |
Conclusion
In conclusion, the free function and delete operator are both mainly used to deallocate dynamically allocated memory. The free function is a library function that is utilized with malloc and calloc. On the other hand, delete is an operator that is utilized to deallocate memory allocated with the new operator.
Difference between free and delete in C++ FAQs
1) Can we use the free function to deallocate memory allocated by new operator in C++?
No, mixing the free function with a new operator leads to undefined behavior. We should utilize the delete operator to deallocate memory allocated with a new function.
2) Is delete operator type-safe in C++?
Yes, delete operator is type-safe and performs operations with the actual type of the object to completely destroy it.
3) Does the free function call destructors of C++ Objects?
No, the free function only releases raw memory and doesn't call any destructors.
4) Can delete be overloaded in C++?
Yes, we can overload the delete operator for custom behavior, but we cannot overload the free function.
5) What is the main difference between the free function and the delete operator in C++?
The main difference between the free function and the delete operator in C++ is that the free function deallocates memory allocated by C-style functions, such as the malloc function. On the other hand, the delete operator deallocates the memory allocated by the new operator and invokes the object's destructor.