calloc(nmemb, size) must allocate nmemb times size bytes. What does the standard additionally guarantee about that multiplication?
Correct answer
D. It fails and returns a null pointer instead of an undersized block
18 views
D. It fails and returns a null pointer instead of an undersized block
18 views
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.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.