2 ms·
While malloc() won't "crash", it can and will, in some circumstances, return NULL. E.g., the following: printf("%p\n", malloc(0x7fffffffffffffff)); prints
by deathanatos 7y ago
While malloc() won't "crash", it can and will, in some circumstances, return NULL. E.g., the following:
printf("%p\n", malloc(0x7fffffffffffffff));
prints
(nil)
on my amd64 machine, as there is no way for the kernel to allocate that much virtual address space. (Even if it wouldn't back it w/ physical RAM.)
The scenario being discussed in this subthread — having a negative number get inadvertently converted into a `size_t` and passed to `malloc()` is exactly the sort of way you end up getting a NULL back. But this is also not guaranteed.