free() in C programming.
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.
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.
It requires only one argument, namely a pointer to the memory location to be released. We cannot use the C free() function to free statically allocated memory (such as local variables) or memory allocated on the stack.
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.
#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.
#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.
Other Topics:
Variables and Identifiers
Relational Operators
if-else statements
Switch case
While Loop
Infinite while Loops
C FOR Loop
Infinite for Loops
Continue in Loops
One Dimensional Array
Two Dimensional Arrays
Read and Display 2D Arrays
Types of functions
Passing Array To Functions
Nesting of Function
Array vs Structure
Array of Structure
Structures and Functions
Structures Within Structures
Use Of Pointers In C
File Handling In C
Loops FAQ
Arrays FAQ
count vowels in a file
Function FAQ
Conditional Statements Assignments
For Loops Assignments
Arrays Assignments
Function Assignments
Structure Assignments
Pointers Assignments
Files Assignments
Storage classes Assignments
Binary Files
count words,lines in a file
Copy files
Update File
Continue in Loops
break in Loops
Difference Between While and Do while
difference while do..while & for
malloc
calloc
Storage Classes
Operators MCQ
Conditional Statements MCQ
Loops MCQ
Arrays MCQ
Function MCQ
Structure MCQ
Pointers MCQ
Files MCQ
Storage classes MCQ