Which statement correctly describes calloc() and malloc()?

Options

  • A. Both always initialize allocated memory to one
  • B. calloc() initializes allocated memory to zero, while malloc() does not initialize it
  • C. malloc() always initializes memory to zero, while calloc() does not
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. calloc() initializes allocated memory to zero, while malloc() does not initialize it

Detailed Explanation

Answer: (B) calloc() initializes allocated memory to zero, while malloc() does not initialize it

The major basic difference between malloc() and calloc() is their initialization behavior.

malloc() allocates a requested amount of memory but does not initialize the contents.

calloc() allocates memory for a number of elements and initializes the allocated bytes to zero.

This distinction is frequently asked in C and C++ programming examinations.