HomeLinuxFree() Operate in C Language

Free() Operate in C Language


In dynamic allocation, a reminiscence area of a sure dimension is reserved at runtime for unique use by the method that allocates it.

The C language has a number of complementary capabilities for dynamic reminiscence administration. Essentially the most generally applied are malloc(), realloc(), and free() that are a part of the usual library and are outlined within the “stdlib.h” header. These three capabilities allocate the reminiscence to a course of, change its dimension, and free it once more.

In programming, it is extremely necessary to strictly handle the dynamic reminiscence as a result of each time we allocate the assets to a program, we take them away from the working system. The mismanagement of dynamic reminiscence can have catastrophic penalties for the processes that run within the system at the moment.

On this Linux Trace article, you’ll learn to use the free() operate which is among the three primary capabilities that the C language supplies for dynamic reminiscence administration that frees the allotted assets and returns them to the working system for reuse.

We’ll present you the syntax and the outline of the way it works, the enter argument, and the information sort that makes it up. Then, we are going to implement using the free() operate in sensible examples with code snippets and pictures which exhibit its use and its results on the system.

Syntax of the Free() Operate in C Language

 

Description of the Free() Operate in C Language

The free() operate frees a specified quantity of dynamically allotted reminiscence.

The dynamic reminiscence is allotted by calling the malloc() operate and specifying the dimensions in bytes of the realm that you simply need to allocate in its enter argument. Because of this, this operate returns the “Ptr” pointer which factors to the newly allotted space.

The free() operate frees the reserved reminiscence area which is referenced by “Ptr” in its single enter argument and returns its availability to the OS. This operate returns no outcomes and has no error definition within the “errno.h” header.

The free() operate is a part of the usual library and is outlined within the “stdlib.h” header. To make use of it and the opposite dynamic reminiscence administration capabilities, you should embody this header in your code as follows:

 

Tips on how to Free the Dynamically Allotted Reminiscence Utilizing the Free() Operate

On this instance, we create a quite simple console software that offers us management over allocating the reminiscence with the malloc() operate and releasing the reminiscence with the free() operate. We then compile the code and run the appliance, monitor the system, and observe the results.

To create the console software, we first embody the required headers and open a predominant() operate with an empty return. We declare the “Ptr” pointer in it which is used within the malloc() and free() capabilities for the tips that could the allotted reminiscence areas. We additionally declare the integer dimension which determines the dimensions of the allotted reminiscence, and the integer possibility which is the situation for the swap that we use to pick out the menu possibility.

As soon as the required variables are declared, the appliance shows their PID utilizing the printf() and getpid() capabilities. Then, this system enters an infinite loop the place it shows a menu with two choices. The user-selected merchandise is retrieved utilizing the scanf() operate within the integer possibility and is the situation for a swap with two circumstances, one for every possibility.

The choice 1 calls the malloc() operate which allocates a 122 MB block of reminiscence to the method. Whereas the choice 2 frees that block by calling the free() operate. You may see the entire code of this software within the following illustration:

#embody <stdio.h>
#embody <stdlib.h>
#embody <unistd.h>
#embody <string.h>

void predominant()
{  
int possibility;
char *Ptr;
int dimension=64000000;
printf(nCourse of ID: %in,getpid());

whereas (1){
    printf (n[1]   Allocate reminiscence.n[2]   Launch reminiscence.n);    
    scanf(“%i”, &possibility);
    swap (possibility){
    case 1:
           printf(nAllocating reminiscence…n);
           Ptr=malloc(dimension*sizeof(char));
           break;
    case 2:
           printf(nReleasing reminiscence…n);
           free(Ptr);
           break;
    }//Finish swap
  }//Finish whereas (1)
}

 

Be aware that in case 1, the malloc() operate allocates a reminiscence space of dimension and returns the “Ptr” pointer. In case 2, the free() operate makes use of the identical pointer as enter argument to free the allotted reminiscence.

To see the results of those two capabilities on the system, we have to open two command consoles. In terminal 1, we run the highest command to see the assets which can be utilized by every system course of. In terminal 2, we compile and run the appliance that we simply noticed.

As soon as the appliance is working, we have a look at the console 1 for the assets which can be utilized by the method whose PID is displayed in console 2.

As we will see within the determine, the system assigned PID quantity 13009 to the method. We are able to see in column 10 of the left terminal that our software consumes lower than 0.1% of the system reminiscence.

If we choose the choice 1 of our software and press “Enter”, the malloc() operate allocates 122 MB of reminiscence to the method.

As we will see on this picture, the reminiscence that’s utilized by our software is now 3.6% of the full reminiscence that’s out there within the system.

Now, we choose possibility 2 of our software which calls the free() operate, passing the “Ptr” pointer as an enter argument. This motion frees the reminiscence that’s allotted by the malloc() operate in possibility 1.

On this picture, we will see how the free() operate freed the allotted reminiscence from 3.6% to 0.1% of the full system that’s utilized by our software.

Conclusion

On this Linux Trace article, we confirmed you use the free() operate to free the dynamically allotted reminiscence. We seemed on the syntax of this operate and the theoretical description of the way it works.

Then, we utilized what we realized in a sensible instance with code snippets and pictures the place we created a console software that controls the calling of the malloc() and free() capabilities. We ran it step-by-step and monitored the system to see the results of calling the capabilities to allocate and free the system reminiscence.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments