Use of free() function in C
Introduction
The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc() functions.
This function is free the memory block. If you want to use any memory location that is not accessible, then you can call the free() function to free the required memory location.
In the C programming language, the memory for the variable is reallocated during the compile time, but the dynamic memory for the variable is not deallocated during the compile time.
It is allocated separately. The free () function used a pointer in a free memory location.
Syntax of the free() function in C language:
Syntax means the order or arrangement in the program. The syntax is also known as the function prototype. Every function has a syntax value. The syntax of the free() function is:
void free (void* ptr);
The free() function is used to pointer the free memory location.
Parameters of the free() function in C language:
The main parameter of the free() function is ptr. Str represents the pointer. The pointer uses to point to the free memory location or block. This blocks are previously allocated by calloc() function, malloc() function or realloc() functions. When the null pointer passes as an argument, then no value occurs.
Returns Value of the free() function in C language:
The free function is mainly used to pointer the free memory location. This function does not return any value. It's only used to find the free memory block.
Header file of the free() function in C language :
Every program contains some header files. For the run of any program, you must need a header file. Without a header file, you can't run any programs. So, we must produce the header file in every program. It is a file with an extension. The free() function is a function which is pointing the free memory address.
The primary use of the header file is to propagate the declaration of code files. The free() function uses as a library function. The header file of the free() function in the C language is <stdlib.h>.
It writes in the program as #include <stdlib.h>.
Example 1 –
Here we were given an example of the free() function using the C language:
//header file initialize
#include<stdio.h>
#include<stdlib.h>
//main code of this program
int main()
{
int*ptr=malloc(20*sizeof(*ptr));
if(ptr!=NULL)
{
*(ptr+4) = 40;
//print the result
printf("The value of the 2nd integer = %d",*(ptr+2));
}
free(ptr);
return (0);
}
Output: We compile and run the above program.The result of this above program is:
The value of the 2nd integer = 40
To better understand, we give another example of the free() function.
Example 2 –
Here we given a example of the free() function using C language:
//header file initialize
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char *str;
//memory allocation
str = (char *) malloc(25);
strcpy (str, “Tutorial and example”);
printf (“String = %s, Address = %u\n”, str, str);
//reallocating memory
str = (char *) realloc(str, 35);
strcat (str, “.com”);
printf (“String = %s, Address = %u\n”, str, str);
//deallocated memory
free (str);
return (0);
}
Output: We compile and run the above program.The result of this above program is:
String = Tutorial and example, Address = 985735479
String = Tutorial and example.com, Address = 985735479
In the above two programs, we have used the free () function; by the programs, we briefly discussed how to write any C language program using the free () function. The free () function is a library function.
So we <stdlib.h> header file in this program.
In a program, if you want to use any memory location but it was not free at that time, then you can call the free() function to free the required memory location. In the above programs, we use stdio.h, stdlib.h and string.h header files.
The primary use of the header file is to propagate the declaration of code files. The output of the two programs is dependent on the memory location. We also share the output of the above two programs.