Which function is used to change the size of a previously allocated memory block in C?

Options

  • A. calloc()
  • B. realloc()
  • C. memset()
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. realloc()

Detailed Explanation

Answer: (B) realloc()

realloc() is used to change the size of a memory block that was previously allocated dynamically.

It can be used when a program needs more or less memory than originally requested.

The memory block may be moved to a different location if sufficient space is not available at its current location.

Therefore, after a successful realloc(), the returned pointer should be used because the memory address may have changed.