C++ free() Function

Overview
The dynamic memory allocated in the C++ with the help of functions like malloc, calloc, realloc is released or, can say, deallocated when the data is no longer needed in the program with the help of function free in C++ so that we can further use that memory location to store another data.
Syntax of C++ free() Function
Syntax of function free in CPP is pretty simple,
Parameters of C++ free() Function
Function free in CPP accepts a pointer.
Further, it deallocates the memory pointed by the pointer and previously allocated by malloc, calloc, or realloc.
There could be two significant scenarios while passing a pointer to a free function.
- NULL :
The free() function does nothing if pointer is null. - Pointer does not point to a memory block allocated by the calloc, malloc, or realloc :
Undefined behavior will occur in this case.
Return Value of C++ free()
There is no return value of function free in CPP.
Digression :
Furthermore, the function's syntax makes it clear because 'void' is written as the return type, and the keyword void before the function name in C function definitions indicates that no return value will be provided to the caller function.
Example
Output:
Explanation:
- The value of ptr is being changed to a garbage value of 1246956856 because of the free() function.
- Integer 58 is no longer stored in the memory.
How does C++ free() Function Work ?
During the lifetime of a program, we need some memory locations to store our data to be used with the algorithms.
So traditionally, we declare variables that get memory in the RAM at the compile-time, but this is just a static memory allocation. What if we need more data locations during runtime?
There is a concept of dynamic memory allocation according to which we can allocate the memory blocks during the program's runtime. The functions like malloc and calloc make it possible. Still, for better memory optimization, we need to deallocate the allocated memory when the data is no longer needed in the program.
Here the free function in CPP comes into the picture, which is used to deallocate the memory space earlier allocated by the malloc, calloc, or realloc functions.
It just accepts the pointer to the memory location, which was earlier returned by the functions responsible for allocating the memory. The memory associated with the pointer is then released, creating the memory block available for us to store new data.
A point to remember is that the free function doesn't change the pointer's value, which means the pointer will still contain the address to that memory location it was pointing to earlier.
More Examples
Working of free() Function
Output:
Explanation:
- In this program, we have declared an integer pointer to store the address of the integer variable.
- Later, we allocate the memory to that pointer with the help of the malloc function, which returns the address of the available memory location of the size provided as a parameter. In our case, we want a memory location equal to the size of int.
- With the help of cin, we take input and store it at the provided address.
- And subsequently, we can see the output of the variable, which means everything is working fine.
- In the end, we have used the free() function by providing the address of the pointer pointing to the location allocated by malloc.
- free() function will deallocate the memory; hence, the pointer will refer to a memory containing a garbage value.
Pointer Still Points to Same Memory Address
Output:
Explanation:
- In this example, we have used calloc to allocate the memory. The difference between malloc and calloc is that we can provide the number of items allocated with the given size.
- In this example, we want a single means 1 location of int size.
- Interesting point to note here is that the free() function does not change the address of the pointer. It remains the same after the execution of the free function.
Conclusion
- free() function is used to deallocate the memory used by the data, which is not currently required in the program.
- Further, those memory locations can store other valuable data.
- If the memory wasn't allocated by malloc, calloc, or realloc, which are used to allocate the memory dynamically, then the free function cannot de-allocate the memory and will show an undefined behavior.
- free in cpp does not change the value of the provided pointer.
- The pointer will still point towards the same memory location.
- Only stuff that the free function in C++ does is to make the memory locations empty.