under no-exception context, I have seen several posts saying
Thing* t = new(std::nothrow) Thing; // returns NULL on failure
if (!t) {
// allocation failure
}
e.g.
How to check memory allocation failures with new operator?
How to find out the return value if my C++ “new” memory allocation failure?
I believe this is the statement they got from C++ standards, as cited:
If the allocation function is declared with a non-throwing exception-specification , it returns null to indicate failure to allocate storage and a non-null pointer otherwise.
However, by Herb Sutter http://www.gotw.ca/publications/mill16.htm
The linux will overcommit memory, and this is not conformant with c++ standards. i.e., checking null is not relevant for linux system. "new" either succeed or fail with process killed by linux.
So what can we do? It seems no way to check the failure.
[UPDATE] about linux overcommit: overcommit_memory
0 — The default setting. The kernel performs heuristic memory overcommit handling by estimating the amount of memory available and failing requests that are blatantly invalid. Unfortunately, since memory is allocated using a heuristic rather than a precise algorithm, this setting can sometimes allow available memory on the system to be overloaded.