C Programming

Free Memory in C with Free Function

The C language is efficient enough to handle its memory management. It means that whenever you declare a regular variable of any data type in C, the programming language itself is responsible for deallocating or releasing this memory once your program has been executed successfully. However, in the case of dynamic memory allocation, since you are allocating the memory manually, that is why you also have to release it on your own.

In the “stdlib.h” library, there is a dedicated function for serving this purpose, i.e., the “free()” function. Today, we will explore the need to use this function in the C programming language. After that, we will look at a few examples in which this function has been used to free up the memory manually in the C programming language in Linux.

Need of using the Free Function in C for Freeing up System Memory:

We all know that our computer systems have limited memory, because of which we can never run infinite programs in it. In the case of automatic memory allocation, the computer takes care of freeing up the memory when your program has finished its execution. However, when we allocate the memory manually from the heap, we have to free it from one way or another.

Otherwise, we will eventually run short of memory, and we will not run our programs any further. This is where the “free()” function of the “stdlib.h” library comes into play. We use this function at the end of a program right before the “return” statement so that the heap memory should be returned to the computer system before the termination of your program.

Suppose you will keep ignoring this function while writing your C codes that are particularly targeted towards dynamic memory allocation. In that case, a point will come when you cannot access your heap any further since it will run out of memory. That is why this seemingly less valuable function must never be forgotten whenever you are taking the responsibility of memory allocation dynamically.

The general syntax of the “free()” function in the C programming language is as follows:

void free (void *ptr)

Here, the “void” keyword before the “free()” function states that the return type of this function is void. The pointer inside the brackets corresponds to the memory location that needs to be deallocated. The following section will describe some examples in which the “free()” function has been used for releasing the dynamic memory acquired in C.

Examples of Using the Free Function in C:

The “free()” function can be used with all the three memory allocation functions of the “stdlib.h” library, i.e., “malloc,” “calloc,” and “realloc.” It is mandatory to use this function after stating the basic functionality of your program so that you can hand over the heap memory that you have dynamically allocated during the program execution back to your computer system. Now, let us take a look at a few examples in which the “free()” function has been used with the “malloc” and “calloc” functions in the C programming language.

Example # 1: Using the Free Function with “calloc” in C:

The C code for using the “free()” function with the “calloc()” function has been shared in the image below:

The basic purpose of this code was to assign some memory dynamically with the help of the “calloc()” function. For that, we have taken the size of the dynamic memory and the values of this dynamic memory as input from the user. Then, we intended to print the acquired values on the terminal. After this whole code, we have our “free()” function that will release the allocated dynamic memory due to the execution of our C program.

A slightly modified version of the same code is also shown in the image cited below:

This modified version only intended to print our dynamically allocated memory values before its manual initialization. We know that the “calloc()” function initializes the whole memory with zeros before the actualization occurs. However, the main point to be noted over here in this code is that even though this code was quite compact and simple, we still made use of the “free()” function once the desired functionality was achieved just to release the heap memory that we had acquired as a result of executing this C program.

Example # 2: Using the Free Function with “malloc” in C:

The C code for using the “free()” function with the “malloc()” function has been shared in the image appended below:

The basic purpose of this code was to assign some memory dynamically with the help of the “malloc()” function. For that, we have assigned the values of this dynamic memory in a “for” loop. Then, we intended to print the acquired values on the terminal with the help of another “for” loop. After this whole code, we have our “free()” function right before the “return” statement that will simply release the allocated dynamic memory as a result of the execution of our C program.

Conclusion:

This article emphasized the importance of using the “free()” function in the C programming language in Linux. Most of the users believe that the allocation functions of the “stdlib.h” file are more important; however, by going through this article, you can clearly see that the “free()” function is equally important. It will help you release the dynamically acquired memory and ensure that you never run out of memory whenever you try to execute your programs in the future.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.