free() Function vs delete Operator in C++

Share Your Love

The difference between the free() function vs delete operator in C++ will be discussed in this topic. So let’s begin.

free() Function:

In C++, the free() method is used to dynamically de-allocate memory. It’s essentially a C library function that’s specified in the stdlib.h header file.

This library function is called when the pointers are either null or point to memory allocated with the malloc() function.

free() Function Syntax:

Assume we’ve declared a pointer ‘ptr‘ and now wish to free up memory for it:

free(ptr); 

The memory of the pointer variable ‘ptr‘ would be de-allocated with the above syntax.

free() Parameters:

ptr is a parameter inside the free() function in the above syntax. The ptr variable is a pointer to a memory block that was allocated with the malloc(), calloc(), or realloc() functions.

It’s also possible that this pointer is null or a malloc pointer that doesn’t point to any other memory block.

  • The free() function will not do anything if the pointer is null.
  • This function will behave in an undefined manner if the pointer is allocated using malloc, calloc, or realloc but does not point to any memory block.

free() Return Value:

There is no value returned by the free() function. Its primary purpose is to free up RAM.

Let’s understand through an example:

#include <iostream>  
#include <cstdlib>  
using namespace std;  
  
int main()  
{  
    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 << "After Freed Garbage Value:" << endl;  
  
    for (int i=0; i<5; i++)  
    {  
        cout << *(ptr+i)<< " ";  
    }  
    return 0;  
}  

Output:

Enter 5 integer:
1
2
3
4
5
User entered value: 
1
2
3
4
5
After Freed Garbage Value:
3424234
2342432
3
4
5

The code above demonstrates how the free() function interacts with malloc ().

First, we declare the integer pointer *ptr, and then we use the malloc() function to allocate memory to this pointer variable.

The uninitialized memory block of 5 integers is now pointed to by ptr. We use the free() function to remove the allocated memory after allocating it.

We obtain a trash value or garbage value when we try to print the value pointed by the ptr, indicating that memory has been de-allocated.

Let’s look at how the free() method interacts with a calloc ():

#include <iostream>  
#include <cstdlib>  
using namespace std;  
int main()  
{  
 float *ptr; // float pointer declaration  
 ptr=(float*)calloc(1,sizeof(float));  
 *ptr=7.48;  
 std::cout << "The value of *ptr before applying the free() function : " <<*ptr<< 	  std::endl;  
 free(ptr);  
 std::cout << "The value of *ptr after applying the free() function :" <<*ptr<< std::endl; 
   return 0;  
}  

Output:

The value of *ptr before applying the free() function : 7.48
The value of *ptr after applying the free() function : 23235345

We can see that the free() function works with a calloc in the example above. The memory block is allocated to the float pointer ptr using the calloc() function. We’ve given the ptr a memory block that can only hold a single float type value.

Let’s start to look at another example:

#include <iostream>  
#include <cstdlib>  
using namespace std;  
int main()  
{  
 int *ptr1=NULL;  
 int *ptr2;  
 int x=9;  
 ptr2=&x;  
 if(ptr1)  
 {  
     std::cout << "Pointer is not Null" << std::endl;  
 }  
 else  
 {  
     cout<<"Ponter is NULL";  
 }  
 free(ptr1);  
 //free(ptr2); // If this statement is executed, then it gives a runtime error.  
 return 0;  
}  

Output:

Ponter is NULL

The preceding code demonstrates how to use the free() method with a NULL pointer. Two pointers, ptr1 and ptr2, have been declared. Pointer ptr1 is set to NULL, while pointer ptr2 is set to the address of the x variable. The memory block assigned to the ptr1 is successfully freed when we use the free(ptr1) function on it.

Because the memory block given to ptr2 is not allocated using the malloc() or calloc() functions, the statement free(ptr2) results in a runtime error.

Delete Operator:

It is a dynamic memory or de-allocation delete operator in the C++ programming language. This operator is mostly used for pointers allocated with the new operator or for NULL pointers.

Syntax:

delete pointer_name;

For instance, suppose we used the new operator to allocate memory to the pointer and now want to delete it. The following statement is used to remove the pointer:

delete p;  

To delete the array, we use the following statement:

delete [] p;  

The following are some key points to note about the delete operator:

  • It can be used to delete arrays or non-array objects that have been allocated with the new keyword.
  • The delete[] and delete operators are used to delete an array or a non-array object, respectively.
  • Because the new keyword allocated memory in a heap, we may claim that the delete operator always frees memory from the heap.
  • It does not destroy the pointer, but it does destroy the value or memory block to which the pointer points.

Let’s look at the simple example of a delete operator.

#include <iostream>  
#include <cstdlib>  
using namespace std;  
  
int main()  
{  
 int *ptr=new int;  
 *ptr=68;  
 std::cout << "The value of p is : " <<*ptr<< std::endl;  
 delete ptr;  
 std::cout <<"The value after delete is : "  <<*ptr<< std::endl;  
 return 0;  
}  

Output:

The value of p is : 68
The value after delete is : 65744549

We used the new operator to allocate memory in the previous code, therefore we used the delete ptr operator to erase the memory block pointed by the pointer ptr.

Let’s look at how to delete an array of objects:

#include <iostream>  
using namespace std;  
int main()  
{  
    int *ptr=new int[5];  // memory allocation using new operator.  
    std::cout << "Enter 5 integers:" << std::endl;  
    for(int i=1;i<=5;i++)  
    {  
        cin>>ptr[i];  
    }  
    std::cout << "Entered values are:" << std::endl;  
    for(int i=1;i<=5;i++)  
    {  
        cout<<*(ptr+i)<<endl;  
    }  
    delete[] ptr; // deleting the memory block pointed by the ptr.  
    std::cout << "After delete, the garbage value:" << std::endl;  
    for(int i=1;i<=5;i++)  
    {  
        cout<<*(ptr+i)<<endl;  
    }  
 return 0;  
}  

Output:

Enter 5 integers: 
1
2
3
4
5
Entered values are:
1
2
3
4
5
After delete, the garbage value:
432423
243432
3
4
5

Differences between delete and free():

In C++, the following are the differences between delete and free():

  • The free() is a method that removes memory at runtime, while delete is an operator that de-allocates memory dynamically.
  • The delete operator deletes a pointer that was allocated using the new operator or a NULL pointer, whereas the free() function deletes a pointer that was allocated with the malloc(), calloc(), or realloc() functions or a NULL pointer.
  • The delete operator in C++ invokes the class’s destructor when it destroys the allocated memory, but the free() function does not; it just frees the memory from the heap.
  • The delete() method is more efficient than the free() method.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 429

Newsletter Updates

Enter your email address below to subscribe to our newsletter