Which function is used to release dynamically allocated memory obtained using malloc(), calloc() or realloc()?

Options

  • A. remove()
  • B. clear()
  • C. free()
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

C. free()

Detailed Explanation

Answer: (C) free()

The C library function free() is used to release memory that was dynamically allocated using functions such as malloc(), calloc() or realloc().

For example:

int* p = (int*)malloc(5 * sizeof(int));

free(p);

The purpose of free() is to return the allocated memory to the system or memory allocator so that it can be reused.