4 ms·
I have never knowingly experienced malloc failing in any of my code which is why I asked if it is something to worry about in practice. Once you run out of memo
by danieltillett 11y ago
I have never knowingly experienced malloc failing in any of my code which is why I asked if it is something to worry about in practice. Once you run out of memory things start dying or the system locks up hard.
Edit. It is impossible for malloc to fail (at least on linux) [1]. Seems rather pointless checking for something that can’t happen.
1. https://scvalex.net/posts/6/ https://scvalex.net/posts/6/
- dalke 11y agoThat link shows that malloc failed, just not due to exceeding available RAM. At the end it says "malloc finally failed because of address space exhaustion."
- danieltillett 11y agoIf the memory was actually being used it would have crashed long before that. The test was kind of artificial.
- dalke 11y agoAbsolutely. I was pointing out that "It is impossible for malloc to fail" is, according to the linked page, an incorrect summary.
- danieltillett 11y agoIt is still pointless checking if malloc fails on linux :) Actually the real issue here is that you should be aware that on some systems malloc might fail and not segfault.
- sovande 11y agoOn virtual memory systems, it is a safe bet that malloc will "never" fail to return a value. It is just good form to check the return value of system calls. If you program for embedded systems (e.g. Arduino) and bring over the bad habit of never checking malloc, you are going to be in for a surprise. I'm not sure, but I believe on iOS, malloc could fail on the device.