5 ms·
(Insert my usual rant about not checking malloc()/calloc() for failure.)
by linuxlizard 12y ago
(Insert my usual rant about not checking malloc()/calloc() for failure.)
- bnegreve 12y agoNote that on many systems (e.g. Linux), malloc/calloc won't always return NULL when you're out of memory because of lazy memory allocation policies. It will only crash when you start reading / writing. That makes it arguably less useful to test the return value. edit: clarity.
- lgeek 12y agoHowever, mmap and the functions which rely on it will return MAP_FAILED/NULL if no gap large enough is found in the virtual address space or if you've used MAP_FIXED and the area isn't free. If you don't check for errors, the application could potentially end up trying to dereference a NULL pointer.
- bnegreve 12y agoOk, let me rephrase then: On modern systems, checking the return value of malloc() is not a proper way to check that the system is out of memory and therefore it doesn't guarantee that the program will run correctly in any case. That's what I wanted to point, and I agree that it remains useful to detect other types of errors such as the ones you mentioned.
- mpu 12y agoIs this a step towards you understanding you are useless?
- linuxlizard 12y agoCheerfully, yes!