Trending October 2023 # How Free() Function Work In C++ # Suggested November 2023 # Top 16 Popular | Restaurant12h.com

Trending October 2023 # How Free() Function Work In C++ # Suggested November 2023 # Top 16 Popular

You are reading the article How Free() Function Work In C++ updated in October 2023 on the website Restaurant12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How Free() Function Work In C++

Introduction to C++ free()

Web development, programming languages, Software testing & others

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++?

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.

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.

using namespace std; int main() {           int *ptr; ptr = (int*) malloc(sizeof(int)); 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()

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

You're reading How Free() Function Work In C++

Update the detailed information about How Free() Function Work In C++ on the Restaurant12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!