free vs delete in C++

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

free() function

The free() function is used in C++ to de-allocate the memory dynamically. It is basically a library function used in C++, and it is defined in stdlib.h header file. This library function is used when the pointers either pointing to the memory allocated using malloc() function or Null pointer.

Syntax of free() function

Suppose we have declared a pointer 'ptr', and now, we want to de-allocate its memory:

The above syntax would de-allocate the memory of the pointer variable 'ptr'.

free() parameters

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

  • If the pointer is null, then the free() function will not do anything.
  • If the pointer is allocated using malloc, calloc, or realloc, but not pointing to any memory block then this function will cause undefined behavior.

free() Return Value

The free() function does not return any value. Its main function is to free the memory.

Let's understand through an example.

The above code shows how free() function works with malloc(). First, we declare integer pointer *ptr, and then we allocate the memory to this pointer variable by using malloc() function. Now, ptr is pointing to the uninitialized memory block of 5 integers. After allocating the memory, we use the free() function to destroy this allocated memory. When we try to print the value, which is pointed by the ptr, we get a garbage value, which means that memory is de-allocated.

Output

free vs delete in C++

Let's see how free() function works with a calloc.

In the above example, we can observe that free() function works with a calloc(). We use the calloc() function to allocate the memory block to the float pointer ptr. We have assigned a memory block to the ptr that can have a single float type value.

Output:

free vs delete in C++

Let's look at another example.

The above code shows how free() function works with a NULL pointer. We have declared two pointers, i.e., ptr1 and ptr2. We assign a NULL value to the pointer ptr1 and the address of x variable to pointer ptr2. When we apply the free(ptr1) function to the ptr1, then the memory block assigned to the ptr is successfully freed. The statement free(ptr2) shows a runtime error as the memory block assigned to the ptr2 is not allocated using malloc or calloc function.

Output

free vs delete in C++

Delete operator

It is an operator used in C++ programming language, and it is used to de-allocate the memory dynamically. This operator is mainly used either for those pointers which are allocated using a new operator or NULL pointer.

Syntax

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

To delete the array, we use the statement as given below:

Some important points related to delete operator are:

  • It is either used to delete the array or non-array objects which are allocated by using the new keyword.
  • To delete the array or non-array object, we use delete[] and delete operator, respectively.
  • The new keyword allocated the memory in a heap; therefore, we can say that the delete operator always de-allocates the memory from the heap
  • It does not destroy the pointer, but the value or the memory block, which is pointed by the pointer is destroyed.

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

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

Output

free vs delete in C++

Let's see how delete works with an array of objects.

Output

free vs delete in C++

Differences between delete and free()

The following are the differences between delete and free() in C++ are:

  • The delete is an operator that de-allocates the memory dynamically while the free() is a function that destroys the memory at the runtime.
  • The delete operator is used to delete the pointer, which is either allocated using new operator or a NULL pointer, whereas the free() function is used to delete the pointer that is either allocated using malloc(), calloc() or realloc() function or NULL pointer.
  • When the delete operator destroys the allocated memory, then it calls the destructor of the class in C++, whereas the free() function does not call the destructor; it only frees the memory from the heap.
  • The delete() operator is faster than the free() function.





Latest Courses