1. Home
  2. Computer Science MCQs
  3. C Programming
  4. What does the C standard guarantee about the return value…

Added 30 Nov -0001

What does the C standard guarantee about the return value of malloc(0)?

  1. Always a null pointer, because zero bytes can never be allocated by any conforming malloc
  2. Always a valid pointer to exactly one byte, because malloc rounds every request up to one byte
  3. Undefined behaviour, so the program is invalid whatever pointer the call happens to return
  4. A null pointer or a unique pointer that can be passed to free are both conforming outcomes Correct answer

Correct answer

D. A null pointer or a unique pointer that can be passed to free are both conforming outcomes

17 views

MCQSEXAM

Explanation

Zero-size allocation is implementation-defined: malloc(0) may return NULL or a unique pointer that cannot be used to access an object but must be freed if it is not NULL. Code that treats a NULL return as failure without checking the requested size can therefore misreport errors. Requesting at least one byte, or guarding the call with a size test, removes the ambiguity.

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