In C++, the free function and the delete operator are primarily used for releasing dynamically allocated memory. Although they serve similar purposes, they differ in various aspects like their source, application, functionality, type-safety, and more.
In this tutorial, we will explore the variance between the free function and the delete operator in C++. Prior to delving into their distinctions, it is crucial to understand the concepts of free and delete in C++.
What is the free function?
In C++, the free function is employed to release dynamically allocated memory. This function, primarily utilized in C++, is declared in the stdlib.h header file. It is utilized when pointers are referencing memory allocated by the malloc function or when pointing to a Null pointer.
Syntax
It has the following syntax:
free(ptr_name);
In this format,
- ptr_name: This indicates the pointer's name.
- Return Value: The free function doesn't have a return value but is primarily used to release memory.
C++ Free Function Example with Malloc
Let's consider a scenario to demonstrate the free function paired 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 instance, we showcase the functionality of the free function in conjunction with the malloc function. Initially, we define an integer pointer *ptr, and proceed to assign memory to this pointer variable using the malloc function. Subsequently, ptr is directed towards the uninitialized memory block containing 5 integers. Following the memory allocation, we employ the free function to release this allocated memory. Upon attempting to display the value pointed to by ptr, we encounter a random value, indicating that the memory has been deallocated.
C++ Free Function Example with Calloc
Let's consider an example to demonstrate the use of 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 instance, we can see that the free function interacts with a calloc function. The purpose of the calloc function is to reserve a memory block for the float pointer. Subsequently, we have allocated a memory block to the pointer capable of storing a float data type value.
What is the Delete Operator?
The delete operator in the C++ programming language is employed to free up dynamically allocated memory. It is primarily utilized for deallocating memory associated with pointers created using the new operator or a NULL pointer.
Syntax
It has the following syntax:
delete pointer_name
To release the allocated memory block, we can utilize the following syntax:
delete p;
To remove the array, we utilize the following statement:
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's consider a scenario to demonstrate 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 instance, we employ the new operator to reserve the memory, thus necessitating the use of the delete ptr operator to release the allocated memory block referenced 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 exist various variances between delete and free functions in C++. The primary variations between free and delete in a Tabular layout are outlined below:
| 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 summary, the free function and delete operator are primarily employed for releasing dynamically allocated memory. The free function is a predefined function commonly used in conjunction with malloc and calloc. Conversely, delete is an operator specifically designed for deallocating memory that was previously allocated using 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, combining the free function with a new operator results in undefined behavior. It is recommended to use the delete operator to release memory allocated with a new function.
2) Is delete operator type-safe in C++?
Yes, the delete operator is type-safe and executes operations based on the actual object type to fully eliminate it.
3) Does the free function call destructors of C++ Objects?
No, the free function solely deallocates memory without invoking any destructors.
4) Can delete be overloaded in C++?
Yes, it is possible to override the delete operator to implement custom functionality, however, it is not possible to override the free function.
5) What is the main difference between the free function and the delete operator in C++?
The primary distinction between the free function and the delete operator in C++ lies in their memory deallocation behavior. While free is responsible for releasing memory allocated by C-style functions like malloc, the delete operator handles memory allocated by new operator and triggers the destructor of the object.