Tutorial Study Image

Free vs Delete in C++


December 20, 2022, Learn eTutorial
640

Although delete and free() have comparable functionality in programming languages, they are not the same. In C++, the delete operator should only be used with pointers that are pointing to memory allocated using the new operator or with the  NULL pointers, but the free() function should only be used with pointers going to memory that are allocated using the malloc() function or with NULL pointers. In this tutorial let us learn about the key differences between the free function and the delete operator in C++.

Free function in C++

The main function of the free function is to release memory that has been allocated during run time. Both the C and C++ programming languages support the use of the free() function.
It frees up dynamically allocated memory which is created by the calloc, relloc, and calloc methods, and offers additional reusable heap memory for future usage. It uses the released pointer's memory as an argument.

C++ uses the free() function to dynamically deallocate memory. It is essentially a C++ library function that is defined in the header file stdlib.h. This library function is mainly used when the pointers are either Null or pointing to memory that has been allocated using the malloc() function or by the Null pointer.

Syntax

Let's assume we've declared a pointer named "ptr," so now  we wish to deallocate its memory:

free(ptr);

The preceding syntax would free the memory allocated to the pointer variable 'ptr'

What are the parameters of the function free() in C++?

Ptr is a parameter that is inside the free() method in the syntax which is mentioned above. The malloc(), calloc(), or realloc function is used to allocate memory, and the ptr is a pointer referring to that memory block. It's also possible for this pointer to be null or to have been allocated using malloc without pointing to any other memory blocks.

  • The free() function can not do anything if the pointer is null.
  • The behavior of this function will be always undefined if the pointer is allocated using malloc, calloc, or realloc but does not point to any memory block.

The return value  of the free() function

No value is returned by the free() function. Its primary objective is to release or free the memory.

How does the free() function work with malloc ()?

Let us look at an example.


#include <iostream>  
#include <cstdlib>  
using namespace std;  
  
int main()  
{  
    int *ptr;  
    ptr = (int*) malloc(7*sizeof(int));  
    cout << "Please enter 7 integer" << endl;  
  
    for (int i=0; i<7; i++)  
    {  
    // *(ptr+i) can be replaced by ptr[i]  
        cin >>ptr[i];  
    }  
    cout << endl << " entered value"<< endl;  
  
    for (int i=0; i<7; i++)  
    {  
        cout <<*(ptr+i)  << " ";  
    }  
    free(ptr);  
  
    /* prints a garbage value just after the ptr is free */  
    cout << "Garbage Value" << endl;  
  
    for (int i=0; i<7; i++)  
    {  
        cout << *(ptr+i)<< " ";  
    }  
    return 0;  
}  

 

Output:


Please enter 7 integer
1
2
3
4
5
6
7
entered value
1 2 3 4 5 6 7 Garbage Value
0 0 3 4 5 6 7

The preceding code demonstrates how the free() function works with malloc (). We first declare the integer pointer *ptr, and then we use the malloc() function to allocate memory to this pointer variable. Ptr now points to an uninitialized memory block of seven integers. Using the free() function, we can delete the memory that has been allocated after it has been used. A garbage value is returned when we attempt to print the value pointed by the ptr, indicating that memory has been deallocated.

How does the free() function work with calloc ()?

Let us look at an example.


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

Output:


Value of *ptr just before applying free() function : 8.7
Value of *ptr just after applying free() function :0

We can see from the sample above that the free() function utilizes a calloc(). The memory block is allotted to the float pointer ptr using the calloc() function. The ptr has been given a memory block that can store a single float type value.

Delete operator in C++

The delete operator does the same task as the free function, mainly freeing up runtime-allocated memory. Programming in C++ typically uses the delete operator.

It is a type of operator that is used to free memory that has been allocated by new keywords like objects and arrays. When the delete function is used with an object in C++, it first executes the object's destructor before allocating the memory that it was using. For a detailed explanation of the delete operator in C++ just check out the C++ memory management tutorial.

Syntax

delete obj;

The key differences between free and delete in C++

Based on Free Delete
Ideal for use the malloc, calloc, and realloc functions to deallocate memory that is already occupied. releasing Memory that has been allocated through the new keyword.
Refers to Free function mainly refers to a library function. It refers to an operator
Overriding It is not possible It is  possible
Speed of execution slow fast
  1. The delete operator is more frequently used in C++ programming, whereas the free function is essentially a library function found in the stdlib.h header file.
  2. After the runtime-allocated memory is released in the free() function, there is no call to the destructor. In the case of the delete operator, however, a call to the destructor is made prior to the release of the allocated memory.
  3. The free() function is slower because, as we all know, it is a function that needs to receive its declaration from the header file. Since the delete operator is an operator—and we all know that operators move more quickly than functions.

Points to remember

  • free is a library function, whereas delete is an operator.
  • Free does not call any destructors but delete calls one if it is there,  based on the size of the array.
  • While delete deals with non-array objects created using new, free deals with any memory block allocated using malloc, calloc, or realloc.
  • new[] creates an array, delete[] deallocates the array.
  • There is no harm done when a null pointer is freed or deleted using free, delete.