free() Vs delete in C++

free() function

The free() function in C++ is primarily used to dynamically deallocate the memory. It is a library-based function and is defined particularly in the stdlib.h header file and is used when the malloc() function is pointing to the memory or NULL pointer.

In other words, free() should only be used when the pointer is pointing using malloc() or a NULL pointer.

Syntax:   

free(pointer);

Here,

free = function to deallocate memory.

pointer = pointer of any data type.

Example:  int *pointer;

          free(pointer);

Let us now look through an example to see how free function works.

#include<bits/stdc++.h>

using namespace std;

int main ()

{

  int *object1, *object2, *object3;

                object1 = (int*) malloc (100*sizeof(int));

                object2 = (int*) calloc (100,sizeof(int));

                object3 = (int*) realloc (object2,500*sizeof(int));

                free (object1);

                free (object3);

  return 0;

}

Output:

1

Explanation:

This program returns nothing. It just shows the ways to allocate and deallocate memory using the free function. The malloc() and calloc() are used to demonstrate the functioning of free() in C++.

For reference, we must know that-

  1. malloc() : used to allocate memory(function)
  2. calloc():allocate and zero-initialize array(function)
  3. realloc() : reallocate memory block(function)

Note: If the pointer does not point to a block previously allocated with calloc(), malloc(), or realloc(), it is not a NULL pointer. It can cause undefined behavior.

Let’s look at another example with NULL pointers.

#include <iostream>

#include <cstdlib>

using namespace std;

int main()

{

                int y = 5;

                int *pointer1 = NULL;

                int *pointer2 = &y;

                if(pointer1)

                {

                                cout << “Pointer is not Null” << endl;

                }

                else

                {

                                cout << “Pointer is Null” << endl;

                }

                free(pointer1);

                cout << *pointer2;

                return 0;

}

Output:

2

Explanation:

In the above code, we have used a NULL pointer to show how the free function works in C++. We have not used calloc(), malloc(), or realloc() functions since we are using a NULL pointer. We have assigned an integer variable ‘y’ with value 5, two integer pointers, namely ‘pointer1’ and ‘pointer2’, and then assigned the address of variable ‘y’ to the pointer2, and the pointer1 is kept NULL. The next task is to check if the pointer1 is NULL or not. If pointer 1 is found NULL, then the output is printed as NULL, and the value of pointer2, which has a base address of y, is printed on the console.

‘delete’ keyword

In C++, the main function of the delete keyword is to destroy array or non-array(pointer) objects which are created by new expressions.

To understand the delete keyword in-depth, look at the following points:

  1. delete keyword is denoted either using delete operator or delete[] operator.
  • delete keyword deallocates the variable on heap memory which is dynamically allocated using a new keyword.
  • When the delete keyword is used, the object is not destroyed instead the value or memory pointed by the pointer is destroyed.
  • It does return value since the delete operator has a void return type.

Syntax:  

delete pointerVariable or pointer_name;

delete [] pointer;  (if it is an array)

Example:   delete *pointer1;

Let’s visualize it using coding examples.

#include<bits/stdc++.h>

using namespace std; 

int main() 

    int *pointer1; 

    pointer1=new int; 

    *pointer1=100; 

        cout << “Current Value : ” <<*pointer1<<endl; 

    delete pointer1; 

        cout <<“Value after deletion : ”  <<*pointer1<<endl; 

 return 0; 

Output:

3

Explanation:

In the above code, we have used the new operator to allocate the memory with the value 100. We already know that the delete operator is used to delete the memory block that is pointed by the pointer1, so we created a new variable that has an address of the block. The value after deletion is then printed on the console.

Let us see how the delete operator works with arrays in the coding example below.

#include<bits/stdc++.h>

using namespace std; 

int main() 

int *pointer1=new int[5];

cout << “Please enter five integers :” <<endl; 

    for(int i=1;i<=5;i++) 

    { 

        cin>>pointer1[i]; 

    cout << “The entered values are:” << endl; 

        for(int i=1;i<=5;i++) 

    { 

        cout<<*(pointer1+i)<<endl; 

    } 

    delete[] pointer1;

    cout << “After deleting the first value, we have :” <<endl; 

        for(int i=1;i<=5;i++) 

    { 

        cout<<*(pointer1+i)<<endl; 

 return 0; 

Output:

4

Explanation:

In the above code, we have constructed a pointer concerning an array that takes 5 elements like input and stores them. To do this, we looped over to take inputs one at a time and display them using consequently by using the second loop. The statement (*pointer+i) indicates that we are shifting the position of the pointer over the elements one by one. We know that while allocating memory, the pointer points to the address of the first element of the array, therefore the delete[] operator also deletes only the first element of the array and prints the rest on the console.

Pin It on Pinterest

Share This