Understanding Free() vs Delete() in C++

In this topic, we will learn about the free() function and the delete operator in C++.

free() Function

In C++, the free() function is used to dynamically release memory. It is a library function used in C++, defined in the stdlib.h header file. This library function is used for pointers pointing to memory allocated using the malloc() function or when it is a null pointer.

👇Click to Claim👇
👉C Language Knowledge Resource Collection

Syntax of free() Function

Assuming we have declared a pointer ‘ptr’, we now want to free its memory:

free(ptr);

The above syntax will free the memory of the pointer variable ‘ptr’.

Parameters of the free() Function

In the syntax above, ptr is the parameter of the free() function. ptr is a pointer to a memory block allocated using malloc(), calloc(), or realloc() functions. This pointer can also be a null pointer or a pointer allocated with malloc but not pointing to any other memory block.

  • If the pointer is a null pointer, the free() function does nothing.

  • If the pointer is allocated using malloc, calloc, or realloc but does not point to any memory block, it will result in undefined behavior. The free() function does not return any value. Its main purpose is to release memory.

Let’s understand this through an example.

#include <iostream>
#include <cstdlib>
using namespace std;
int main(){   int *ptr;   ptr = (int*) malloc(5*sizeof(int));   cout << "Enter 5 integers" << endl;
   for (int i=0; i<5; i++)  {       // *(ptr+i) can be replaced by ptr[i]       cin >>ptr[i];  }   cout << endl << "User entered values" << endl;
   for (int i=0; i<5; i++)  {       cout <<*(ptr+i)  << " ";  }   free(ptr);
   /* prints a garbage value after ptr is freed */   cout << "Garbage Value" << endl;
   for (int i=0; i<5; i++)  {       cout << *(ptr+i)<< " ";  }   return 0;}

The above code demonstrates how the free() function works with malloc(). First, we declare an integer pointer *ptr, then use the malloc() function to allocate memory for this pointer variable. Now, ptr points to an uninitialized memory block for 5 integers. After memory allocation, we use the free() function to destroy this allocated memory. When we try to print the value pointed by ptr, we get a garbage value indicating that the memory has been freed.

Output:

Understanding Free() vs Delete() in C++

Next, let’s look at an example using calloc.

#include <iostream>
#include <cstdlib>
using namespace std;
int main(){   float *ptr; // Float pointer declaration   ptr = (float*)calloc(1,sizeof(float));   *ptr=6.7;   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;}

In the above example, we can observe how the free() function works with calloc(). We used the calloc() function to allocate a memory block for the float pointer ptr. We allocated a memory block that can hold a single float type value.

Output:

Understanding Free() vs Delete() in C++

Let’s 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<<"Pointer is NULL";  }   free(ptr1);   //free(ptr2); // If this statement is executed, a runtime error will occur.   return 0;}

The above code demonstrates how the free() function works with a null pointer. We declared two pointers, ptr1 and ptr2. We assigned a NULL value to pointer ptr1 and assigned the address of variable x to pointer ptr2. When we apply the free(ptr1) function, the memory block allocated to ptr is successfully freed. The free(ptr2) statement will cause a runtime error because the memory block pointed to by ptr2 was not allocated using malloc or calloc.

Output:

Understanding Free() vs Delete() in C++

Delete Operator

The delete operator is used in the C++ programming language to dynamically release memory. It is mainly used to delete pointers allocated with the new operator or null pointers.

Syntax

delete pointer_name

For example, if we have allocated memory for a pointer using the new operator and now we want to delete it. To delete the pointer, we use the following statement:

delete p;

To delete an array, we use the following statement:

delete [] p;

Some Important Points Related to the Delete Operator:

  • It is used to delete arrays or non-array objects allocated with the new keyword.

  • To delete arrays or non-array objects, we use delete[] and delete operators respectively.

  • The new keyword allocates memory in the heap, so it can be said that the delete operator always releases memory from the heap. It does not destroy the pointer but destroys the value or memory block pointed to by the pointer.

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

#include <iostream>
#include <cstdlib>
using namespace std;
int main(){   int *ptr;   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;}

In the above code, we allocated memory using the new operator, so we use the delete ptr operator to destroy the memory block pointed to by the pointer ptr.

Output:

Understanding Free() vs Delete() in C++

Let’s see how to use the delete operator with an array of objects.

#include <iostream>  using namespace std;  int main()  {      int *ptr=new int[5];  // Allocating memory using the new operator.    std::cout << "Enter 5 integers:" << std::endl;      for(int i=1;i<=5;i++)      {          cin>>ptr[i];      }      std::cout << "Entered values:" << std::endl;          for(int i=1;i<=5;i++)      {          cout<<*(ptr+i)<<endl;      }      delete[] ptr; // Delete the memory block pointed to by the pointer.    std::cout << "Garbage value after delete:" << std::endl;          for(int i=1;i<=5;i++)      {          cout<<*(ptr+i)<<endl;      }   return 0;  }

Output:

Understanding Free() vs Delete() in C++

Differences Between Delete and Free()

The differences between delete and free() in C++ are as follows:

  • delete is an operator for dynamically releasing memory, while free() is a function for destroying memory at runtime.

  • The delete operator is used to delete pointers that can be pointers allocated with the new operator or null pointers, while the free() function is used to delete pointers allocated with malloc(), calloc(), or realloc() functions or null pointers.

  • When the delete operator destroys allocated memory, it calls the destructor of the C++ class, while the free() function does not call the destructor; it only releases memory in the heap.

  • The delete operator is faster than the free() function.

Understanding Free() vs Delete() in C++



 Popular Recommendations
  • CLion Tutorial – Offline Work in CLion

  • C Language Algorithm – “Square Root of x” Algorithm Problem

  • C++ Tutorial – Detailed Explanation of the Difference Between malloc() and new in C++

Leave a Comment