Delete and Free in C++



In C++, both delete and free() are used to deallocate the dynamically created memory. In this article, we will learn about the delete operator and free() function with the help of examples.

C++ delete Operator

The delete operator is used to deallocate the memory. User has privilege to deallocate the created pointer variable by this delete operator.

Syntax

Here is the syntax of delete operator in C++ language:

delete pointer_variable;

Here is the syntax to delete the block of allocated memory:

delete[ ] pointer_variable;

Example

Following is the example of delete operator to see its usage in memory deallocation:

#include<iostream>
using namespace std;

int main () {

   // Pointer to integer
   int *ptr1 = NULL;
   
   // Dynamically allocate memory for int
   ptr1 = new int;               

   // Pointer to float with initialization
   float *ptr2 = new float(299.121);

   // Pointer to array of 28 integers
   int *ptr3 = new int[28];

   *ptr1 = 28;                   

   cout << "Value of pointer variable 1 : " << *ptr1 << endl;
   cout << "Value of pointer variable 2 : " << *ptr2 << endl;

   if (!ptr3)
      cout << "Allocation of memory failed\n";
   else {
      // Assign values to part of the array
      for (int i = 10; i < 15; i++)
         ptr3[i] = i + 1;

      cout << "Value of store in block of memory: ";
      
      // Print the assigned values
      for (int i = 10; i < 15; i++)
         cout << ptr3[i] << " ";
   }

   // Free dynamically allocated memory
   delete ptr1;
   delete ptr2;
   delete[] ptr3;

   return 0;
}

The above program produces the following result:

Value of pointer variable 1 : 28
Value of pointer variable 2 : 299.121
Value of store in block of memory: 11 12 13 14 15 

Here,

  • In this example, three pointer variables are declared as ptr1, ptr2, and ptr3. The pointer variables ptr1 and ptr2 are initialized with the value using new and ptr3 is storing the allocated block of memory by new operator.
  • Elements of array are printed by user and sum of elements is printed. To delete the allocated memory; delete ptr1, delete ptr2 and delete[] ptr3 are used.

C++ free() Function

The function free() is used to deallocate the allocated memory by malloc(). It does not change the value of pointer which means it still points the same memory location.

Syntax

Following is the basic syntax of free() function:

void free(void *pointer_name);

Here,

  • pointer_name : Any name given to the pointer.

Example

In this example, we will learn the usage of free() in memory deallocation:

#include <stdio.h>
#include <stdlib.h>

int main() {
   int n = 4, i, *p, s = 0;

   // Allocate memory for 4 integers
   p = (int*) malloc(n * sizeof(int));

   if(p == NULL) {
      printf("\nError! memory not allocated.");
      exit(0);
   }

   // Assign values manually
   p[0] = 10;
   p[1] = 20;
   p[2] = 30;
   p[3] = 40;

   // Calculate sum
   for(i = 0; i < n; ++i) {
      s += *(p + i);
   }

   printf("\nSum : %d", s);

   // Free allocated memory
   free(p);

   return 0;
}

The above program produces the following result:

Sum : 100

Difference Between delete and free()

Below is the difference table to learn the usage of delete() in C++ and free() in C:

Feature free() delete
Language C and C++ C++ only
Program uses malloc(), calloc() new or new[]
Syntax free(ptr); delete ptr; / delete[] ptr;
Constructor/Destructor Handling No constructor/destructor is called. Calls destructor if object has one.
Type Safety No (void*) support Yes (type-safe)
Updated on: 2025-06-03T11:19:24+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements