Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

(Insert my usual rant about not checking malloc()/calloc() for failure.)


Note 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.


However, 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.


Ok, 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.


Is this a step towards you understanding you are useless?


Cheerfully, yes!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: