Updated April 12, 2023
Introduction to C++ free()
free() function in C++ <cstdlib> library is used to deallocate a memory block in C++. Whenever we call malloc, calloc or realloc function to allocate a memory block dynamically in C++, compiler allocates a block of size bytes of memory and returns a pointer to the start of the block. The new memory block allocated is not initialized but have intermediate values. free() method is used to free such block of memory. In case the pointer mentioned does not point to any memory block then it may lead to an undefined behavior, but does nothing in case of null pointer. Also after the memory block is made available still the pointer points to the same memory location.
Syntax:
void free(void *ptr)
- Here ptr refers to a pointer pointing to memory block in C++ that has been previously allocated by malloc, calloc or realloc. Here type of pointer is void because it is capable to hold any type of the pointer and can be cast to any type while dereferencing.
- In case pointer mentioned in free function is a null pointer then function does nothing as there is memory block for it to deallocate and returns nothing.
- And in case the pointer points to a memory block that has not been allocated using any one of malloc, calloc or realloc method then the behavior of free function can not be predicted.
Return Type:
The return type of free() function is void, that means this function returns nothing. All it does is simply deallocating the block of memory pointed by the referred pointer.
How free() Function work in C++?
- Free method is a great tool for dynamic memory management. It is present in <cstdlib> header file.
- When a memory block is allocated using std::malloc, std::calloc or std::alloc.a pointer is returned. This pointer is passed to free function, for deallocation. This helps in memory management for the compiler dynamically.
- In case the pointer is a null pointer then function does nothing as there is no memory being referenced by the pointer.
- As the datatype for the pointer is void then its is capable for dereferencing any type of pointer.
- In case the value of the pointer mentioned is not one allocated using these three methods then behavior of free function is undefined. Also it is undefined if the memory block being referenced by the pointer has already been deallocated using std::free or std::realloc method.
- This method has no impact on the pointer it just frees the memory block, pointer keep referring to the memory block.
- All the dynamic memory allocation and deallocation methods work in synchronize manner so that memory block being referred by the pointer for allocation must be free at that time.
Examples of C++ free()
Given below are the examples mentioned:
Example #1
In this example we use usecalloc method to allocate memory to a value dynamically. Then we use free method to deallocate the memory and see what happens to the pointer and the value being referenced.
Code:
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main()
{ int *myPtr;
myPtr = (int*) calloc(1,sizeof(int));
*myPtr = 10;
int* myPtr2 = (int*)std::calloc(10, sizeof *myPtr);
int *ptr3 = new int;
cout<< "Before executing freeing" <<endl<<endl;;
cout<< "Address for myPtr1= " <<myPtr<<endl;
cout<< "Value for myPtr1= " << *myPtr<<endl<<endl;
cout<< "Address for myPtr2 = " << myPtr2 <<endl;
cout<< "Value for myPtr2= " << *myPtr2 <<endl<<endl;
cout<< "Address for ptr3 = " << myPtr2 <<endl;
cout<< "Value for ptr3= " << *myPtr2 <<endl<<endl;
free(myPtr);
free(myPtr2);
free(ptr3);
cout<< "After executing freeing" <<endl<<endl;;
/* ptr remains same, *ptr changes*/
cout<< "Address for myPtr1 = " <<myPtr<<endl;
cout<< "Value for myPtr1= " << *myPtr<<endl<<endl;
cout<< "Address for myPtr2= " << myPtr2 <<endl;
cout<< "Value for myPtr2= " << *myPtr2 <<endl<<endl;
cout<< "Address for ptr3 = " << myPtr2 <<endl;
cout<< "Value for ptr3= " << *myPtr2 <<endl<<endl;
return 0;
}
Output:
Example #2
In this example, we allocate the memory using std::malloc and then reallocate using std::realloc method. After this memory block is deallocated and then its pointer and value being stored in memory block referenced by the pointer is observed.
Code:
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main()
{ int *ptr;
ptr = (int*) malloc(sizeof(int));
cin>> *(ptr);
cout<< "Value in memory block before executing free function is "<< *(ptr) <<endl;
free(ptr);
cout<< "Value in memory block before executing free function is " ;
cout<< *(ptr) <<endl;
char *ptr1;
ptr1 = (char*) malloc(10*sizeof(char));
strcpy(ptr1,"Lets see how free works");
cout<< "Value in char pointer is : " << ptr1 <<endl;
ptr1 = (char*) realloc(ptr1,20);
strcpy(ptr1,"free functon is terrific");
cout<< "After reallocating value in char pointer is : " <<ptr1 <<endl;
free(ptr1);
cout<<endl<< "After executing free on char pointer : " << ptr1;
return 0;
}
Output:
Advantages of C++ free()
Given below are the advantages:
- This method helps is dynamic memory management.
- It helps to reuse the memory blocks that are not being used further. Since only the storage being referenced by the mentioned pointer is modified, this has no impact on other memory allocations.
- All dynamic allocation (malloc, calloc or realloc )and deallocation(free) methods takes care that memory allocations to same memory blocks occurs after the deallocations of those memory blocks.
Conclusion
Free method is used to deallocate the memory blocks dynamically that is being referenced by the specified pointer. This memory being referenced must be allocated using malloc, calloc or realloc method. In case it is not then method behavior is undefined. In case it is null pointer then nothing happens. Thus it is great utility for dynamic memory management.
Recommended Articles
This is a guide to C++ free(). Here we discuss how free() function work in C++, advantages and respective programming examples. You may also have a look at the following articles to learn more –