Practice mode C Language

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.