Which statement about realloc() is correct?

Options

  • A. It can change the size of an existing dynamically allocated memory block
  • B. It is used only to initialize memory to zero
  • C. It is a C++ constructor
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

A. It can change the size of an existing dynamically allocated memory block

Detailed Explanation

Answer: (A) It can change the size of an existing dynamically allocated memory block

realloc() is used to resize a memory block that was previously allocated dynamically.

For example, a program may initially allocate memory for 5 elements and later need space for 10 elements. realloc() can be used to request a new size.

The memory may be moved to another location if the existing location cannot accommodate the new size. Therefore, the returned pointer from realloc() is important.

realloc() does not serve the same purpose as calloc() and is not a C++ constructor.