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

Discussion

Which of the following is/are true

  • A.calloc() allocates the memory and also initializes the allocates memory to zero, while memory allocated using malloc() has random data.
  • B.malloc() and memset() can be used to get the same effect as calloc().
  • C.calloc() takes two arguments, but malloc takes only 1 argument
  • D.All of the above

Answer: D

Correct answer: All of the above

All three statements are correct. calloc zeroes the memory it returns while malloc leaves whatever the allocator hands over, which is why an uninitialised malloc block holds arbitrary data.

malloc followed by memset to zero achieves the same result, which is essentially what calloc does internally. And the signatures differ: calloc(n, size) takes a count and an element size, while malloc(size) takes a single total.

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

Post your comment

Please log in or register to post a comment.