2 ms·
Why couldn't you malloc them? Malloc doesn't care about types, it just wants the number of bytes, and 0 is a perfectly valid value to pass to malloc. Now, it's
by int_19h 1y ago
Why couldn't you malloc them? Malloc doesn't care about types, it just wants the number of bytes, and 0 is a perfectly valid value to pass to malloc. Now, it's unspecified whether malloc will give you back a pointer distinct from all other non-freed allocated blocks or NULL, but if you don't rely on address as identity that is irrelevant.
- kragen 1y agoYou're right, in the standard it's implementation-defined. I mistakenly thought malloc(0) was undefined. That said, it's probably not the best-tested code path in the system library.
- tonyarkles 1y agoAnd correspondingly calling free() on the returned value is also unlikely to be particularly well tested.
- kragen 1y agoWell, unless it's NULL. I'm pretty sure that people call free(0) a lot.
- int_19h 1y agoI mean, these are literally all special values that are explicitly called out in all the relevant standards (both ISO C and POSIX). Anyone who is competent and is writing tests for libc would surely cover that.
- kragen 1y agoProbably malloc(0) is not completely untested, yes. But it's unlikely to be regularly executed by applications.