220k views
5 votes
What does malloc() return if it fails to allocate the requested memory?

a) 0
b) -1
c) Null pointer
d) Undefined behavior

User Jeekim
by
7.2k points

1 Answer

5 votes

Final answer:

If malloc() fails to allocate the requested memory, it returns a null pointer, which serves as an indication of the failure to the calling code.

Step-by-step explanation:

When the malloc() function in C fails to allocate the requested memory, it returns a null pointer. The null pointer is a special value that indicates that no memory was allocated. This is typically used as a signal to the calling code that the memory allocation did not succeed, thereby allowing the code to handle the error appropriately, such as by cleaning up resources or reporting an error message to the user.

User Alexander Egger
by
8.1k points