Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the main difference between malloc() and calloc() ?

  • A.calloc() allocates from the stack while malloc() uses the heap
  • B.calloc() initialises the allocated memory to zero while malloc() does not
  • C.calloc() can allocate only a single block while malloc() can allocate many
  • D.calloc() returns an int pointer while malloc() returns a void pointer

Answer: D

Correct answer: calloc() initialises the allocated memory to zero while malloc() does not

calloc() takes the number of elements and the size of each, and clears the whole block to zero before returning it. malloc() takes a single size in bytes and leaves the contents indeterminate, so reading them before assignment is undefined behaviour.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.