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.Both malloc() and calloc() return 'void *' pointer
  • D.All of the above

Answer: D

Correct answer: All of the above

All three statements hold. calloc zeroes what it returns while malloc leaves arbitrary data; malloc followed by memset reproduces that effect; and both return void *.

One difference the list does not mention is worth knowing: calloc multiplies its two arguments internally and is required to detect overflow in that product, whereas a hand-written malloc(m * n) can wrap silently and allocate far too little - a well-known source of security bugs.

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

Post your comment

Please log in or register to post a comment.