In C programing , free() is a library function located in the stdlib.h header file and used to free or release space allocated by functions such as malloc() ,calloc( ),realloc() and avoid memory wastage.

This helps reuse memory, thus making our program more efficient. Since memory is not de-allocated by itself, we have to use the free() function to de-allocate memory during program run-time.

When you de-allocate a block of memory with the help of free() function, it creates a dangling pointer where the pointer contains a garbage address value. if dynamic memory is not freed using the free() function until program execution is complete, a memory leak may occur.
from fig. let understand.
by using the calloc() Dynamically 40 bytes of memory space has been allocated to store 10 integer numbers.
after use of memory .it is releaased using the free() function.



Syntax: Syntax of free() function in C is shown below:

void free(void *ptr);

1. The free() function takes a pointer parameter which is a pointer to the memory block that needs to be freed or deleted.

2. The return type of free() is void which means the function returns no value.


C program shows the use of the calloc() function to allocate memory dynamically and the free() function.

#include <stdio.h>
#include <stdlib.h>
int main()
 {
     // The pointer ptr is used to hold the
  // base address of the block created
  int* ptr;
  int no=0;
  // Accepts the total number of elements for the array
   printf("Enter number of Elements:\n”);
  scanf("%d", &no);
  // Dynamically allocate memory using calloc()
   ptr = (int*)calloc(no, sizeof(int));
   // Check if the memory is successfully
  // allocated by calloc() function
   if (ptr == NULL)
    {
    printf("\n Fail to Allocate Memory! \n");
    exit(0);
    }
   // Memory has been Successfully allocated using calloc()
   printf("\n Memory Has been allocated Successfully using \"calloc()\”. \n");
  // Release or free the memory
  free(ptr);
   printf("\n The memory allocated using Calloc has Successfully freed.");
  return 0;
 }
 Output:
  Enter number of Elements: 10
 Memory Has been allocated Successfully using "calloc()".
 The memory allocated using Calloc has Successfully freed.


C program to create dynamic array and add an element to a array:

#include <stdio.h>
#include <stdlib.h>
int main()
{
 int size = 10;
  int *my_arr = (int*) calloc(size * sizeof(int));
  int i,n;
  printf(“\n Enter %d number to add in array\n”);
  for(i = 0; i< size; i++)
  {
  scanf(“%d”,&n);
  my_arr[i] = n;
  }
 // Add a new element to the array
  printf(“\n Add new Element to an array\n”);
  scanf(“%d”,&n);
  size++;
  my_arr = (int*) realloc(my_arr, size * sizeof(int));
 my_arr[size-1] = n;
 printf(“\n Your Array Elements are here\n”);
  for(i = 0; i< size; i++)
  {
   printf("%d ", my_arr[i]);
 }
  free(my_arr);
  return 0;

}



Enter 10 number to add in array
 12 54 14 76 22 89 33 90 65 19

Add new Element to an array
100
Your Array Elements are here
 12 54 14 76 22 89 33 90 65 19 100

Explanation:
In this example, we first create a dynamic array of size 10 using the Malloc() function. It then sets each element of the array at its index using a for loop entered by user. To add a new element to an array, we increase the size of the array by one and have use the realloc() function to resize the array. We set the value of the last element in the array to the new value entered by user.


Previous Topic:-->> calloc() in C || Next topic:-->>realloc() function in C.