Hacker News new | ask | show | jobs
by theseoafs 3465 days ago
They surely do not. Malloc is specified in such a way such that the request for memory can fail (which leads to returning NULL).
2 comments

The specification allows for this, yes. However, on some platforms (including linux glibc by default, I believe), malloc() never fails, but allocates virtual memory optimistically; the first you hear of an out of memory condition is when the system slows down due to paging, and the next thing you notice is when the OOM killer nixes a process.

Of course, other platforms, especially embedded ones, behave differently.

Actually there is one reason for malloc to return NULL even with virtual memory, your process can run out of address space.
Or run out of room in the paging file. Your addressable memory cannot be larger than physical memory without a backing store.
Depending on VM_OVERCOMMIT_MEMORY, Linux might give out address space well beyond the size of the pagefile, hoping many of those pages are never written to (e.g., most threads never get anywhere near the bottom of their default stacks).
Assuming that the OS APIs used by malloc() do tell the application about OOM.