3 ms·
Have you met programmers? Less snarkily, there are billions of lines of production code that don't check return values for malloc(), printf(), and so on becaus
by pjc50 10y ago
Have you met programmers?
Less snarkily, there are billions of lines of production code that don't check return values for malloc(), printf(), and so on because those failures are extremely rare and difficult to handle sensibly.
Banning dynamic allocation would ban large areas of programming language - Lisp, Python, Javascript, and so on.
- kazinator 10y agoEven if you check the return value of malloc, that doesn't help when virtual memory is overcommitted. Malloc gets a valid pointer from mmap, but accessing the memory later crashes. A system OOM may actually show a soft behavior, whereby the whole system thrashes more and more until it grinds to a halt. Whatever your program does is futile and irrelevant. To QA the program for how it handles null out of malloc, you need to tweak the setup: disable overcommit and simulate low memory conditions. (Or even just switch to an alternative allocator that simulates failures.) Checking the result of malloc for null is of course good for maximal portability: your code can be reused where the check actually means something. Checking for null and not testing that logic is almost no better than not testing at all (in keeping with the general hypothesis that untested code is junk).