What is the purpose of the free()
function in C?
a) To deallocate memory previously allocated by
malloc()
b) To allocate memory
c) To clear a variable
d) To reset a pointer
Answer:
a) To deallocate memory previously allocated by
malloc()
Explanation:
The free()
function in C is used to deallocate memory that was previously allocated using malloc()
, calloc()
, or realloc()
. This function returns the memory back to the system for future use, preventing memory leaks, which occur when memory is not properly released. After calling free()
, the pointer used to allocate the memory should not be used again unless it is reassigned.
Proper use of free()
is essential for managing memory in C, especially in long-running applications or those that handle a significant amount of dynamic memory allocation.