1. Home
  2. Computer Science MCQs
  3. C Programming
  4. calloc(nmemb, size) must allocate nmemb times size bytes…

Added 30 Nov -0001

calloc(nmemb, size) must allocate nmemb times size bytes. What does the standard additionally guarantee about that multiplication?

  1. It wraps the product modulo SIZE_MAX and allocates that size
  2. It aborts the process, because calloc treats overflow as a fatal error
  3. Nothing; overflow is undefined behaviour that the caller must prevent
  4. It fails and returns a null pointer instead of an undersized block Correct answer

Correct answer

D. It fails and returns a null pointer instead of an undersized block

18 views

MCQSEXAM

Explanation

calloc multiplies its two arguments and must return NULL if the product would overflow size_t, so it can never hand back a block smaller than the request. This is one reason calloc(nmemb, size) is safer than malloc(nmemb * size), where the multiplication happens in the caller and can wrap silently. calloc also zeroes every byte of the block, unlike malloc.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs