5 ms·
It's been a while but while I agree the man page says that, my limited understanding was the typical libc on linux won't really return NULL under any sane scena
by sidewndr46 10mo ago
It's been a while but while I agree the man page says that, my limited understanding was the typical libc on linux won't really return NULL under any sane scenario. Even when the memory can't be backed
- LordGrey 10mo agoI think you're right, but "typical" is the key word. Embedded systems, systems where overcommit is disabled, bumping into low ulimit -v settings, etc can all trigger an immediate failure with malloc(). Those are edge cases, to be sure, but some of them could be applied to a typical Linux system and me, as a coder, won't be aware of it. As an aside: To me, checking malloc() for NULL is easier than checking a pointer returned by malloc on first use. That's what you're supposed to do in the presence of overcommit.
- nextaccountic 10mo agoEven with overcommit enabled, malloc may fail if there is no contiguous address space available. Not a problem in 64 bits but may occasionally happen in 32 bits
- Bjartr 10mo agoBut why would you want to violate the docs on something as fundamental as malloc? Why risk relying on implementation specific quirks in the first place?
- im3w1l 10mo agoBecause it's orders of magnitudes easier not to handle it. It's really as simple as that.