Machine-translated by Google. Questions are authored in English.
Top

Discussion

malloc() returns a NULL if it fails to allocate the requested memory.

  • A.TRUE
  • B.FALSE
  • C.May Be
  • D.Can't Say

Answer: A

Correct answer: TRUE

When malloc cannot satisfy a request it returns a null pointer. That is the only way it reports failure - there is no error code and nothing is set beyond, on most systems, errno.

Checking the return value is therefore the caller's responsibility, and omitting the check is a common fault: the program proceeds to dereference a null pointer and crashes at a line some distance from the real cause. Note that malloc(0) may legitimately return either null or a unique pointer.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.