1. Home
  2. Computer Science MCQs
  3. C Programming
  4. What does realloc(p, 0) do when p is a valid block…

Added 30 Nov -0001

What does realloc(p, 0) do when p is a valid block returned by malloc?

  1. It is undefined behaviour, since realloc is never permitted a zero size at all
  2. Implementation-defined: it may free p and return NULL, or return a small block Correct answer
  3. It always frees p and returns NULL, which the caller must then not free again
  4. It always returns NULL and leaves the original block p unchanged and valid

Correct answer

B. Implementation-defined: it may free p and return NULL, or return a small block

18 views

MCQSEXAM

Explanation

With a size of zero, realloc has implementation-defined behaviour: the block may be freed and a null pointer returned, or the implementation may return a unique non-null pointer that can be passed to free. Portable code tests for zero explicitly and calls free instead. Note that a failed realloc with a nonzero size returns NULL and leaves the original block untouched and still valid.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? 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