free() Function in C Library With Examples
When memory blocks are allotted by calloc(), malloc(), or realloc() functions, the C library function free() is used to deallocate or release the memory blocks to reduce their wastage.
free() function in C should only be used either for the pointers pointing to the memory allocated using malloc() or for a NULL pointer. free() function only frees the memory from the heap and it does not call the destructor. To destroy the allocated memory and call the destructor we can use the delete() operator in C.
Syntax to Use free() function in C
void free(void *ptr)
Here, ptr is the memory block that needs to be freed or deallocated.
For example, program 1 demonstrates how to use free() with calloc() in C and program 2 demonstrates how to use free() with malloc() in C.
Enter number of Elements: 5 Successfully allocated the memory using calloc(). Calloc Memory Successfully freed.
Enter number of Elements: 5 Successfully allocated the memory using malloc(). Malloc Memory Successfully freed.
Please Login to comment...