free vs delete in C++
In this topic, we are going to learn about the free() function and delete operator in C++.
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'.
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.
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.
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.
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.
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.
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:
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.
Let's see how delete works with an array of objects.
Differences between delete and free()
The following are the differences between delete and free() in C++ are: