What does malloc() return if it cannot allocate the requested memory?
Correct answer D. A null pointer
Explanation
On failure malloc returns a null pointer, so the result should always be checked before use, for example if (p == NULL). It does not end the program or return -1, and dereferencing the null pointer is undefined behaviour. Calling malloc(0) is a separate case: it may return either NULL or a unique pointer that must not be dereferenced.
Answer checked How we write and check questions
Discussion