What does the C standard guarantee about the return value of malloc(0)?
Correct answer
D. A null pointer or a unique pointer that can be passed to free are both conforming outcomes
17 views
D. A null pointer or a unique pointer that can be passed to free are both conforming outcomes
17 views
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.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.