Free vs delete() in C++

In this section, we will learn about the free() function and also create a C ++ program of the delete operator.

What is free() Function in C++?

In C++, the free() function is used to dynamically de-allocate the memory. It is essentially a library feature used in C++ and is specified in the header file stdlib.h. This library function can be used when either the pointers refer to the allocated memory using malloc() or the Null pointer.

Syntax of free() function

Assume that we defined a ‘ptr’ pointer, and now we want its memory to be de-allocated:

The syntax above will de-allocate the pointer variable ‘ptr’ memory.

free() parameters

In the syntax above, ptr is a parameter within the function free(). The ptr is a pointer pointing towards the allocated memory block using malloc(), calloc() or realloc() feature. Even this pointer may be null or a pointer allocated using malloc but not referring to any other block of memory.

  • If the pointer is null, then the function free() will do nothing.
  • Unless the pointer is allocated using malloc, calloc, or realloc but does not point to any main memory, then this feature can trigger undefined behavior.

free() Return Value

The function free() refused to return any value. The key feature is memory free.

By an example, let’s understand.

The code above demonstrates how function free() works with malloc(). First, we define the integer pointer * ptr, and then we assign the memory to that pointer variable using malloc(). Now, ptr points to the uninitialized 7-integer memory block. We use the free() function after allocating the memory to delete this allocated memory.

Output:

Free vs delete() in C++

Example: malloc() and free()

Output:

Free vs delete() in C++

Example 3: realloc()

Output:

Free vs delete() in C++


Delete Operator

It is an operator used in programming language for C++, and it is used to dynamically de-allocate the memory. This operator is primarily used for the allocation of many references using a new operator or NULL pointer.

Syntax

For example, if we use the new operator to allocate the memory to the pointer, then we want to erase it now. We use the following statement to delete the Pointer:

Some significant delete operator related points are:

  • Either it is used to delete the array or non-array objects which are allocated using the new keyword.
  • We use delete [] and delete operator to delete the array or non-array object, respectively.
  • The latest keyword allocates the memory in a heap, and we can assume the delete operator must still deallocate the memory from the heap.
  • It does not destroy the pointer, but it destroys the value or the block of memory which the pointer points to.

Example of delete operator:

Output:

Free vs delete() in C++

We have illustrated the use of new and delete operators in the example of the code above. We used the “new” operator to allocate memory for a variable, arrays, and also initialize a value for another variable. Then we use delete operator to delete these entities.

Pin It on Pinterest

Share This