3 ms·
I'd argue that zero-length buffer is far from "perfectly well-defined". Quoting C99: " If the size of the space requested is zero, the behavior is implementati
by kbart 11y ago
I'd argue that zero-length buffer is far from "perfectly well-defined". Quoting C99:
" If the size of the space requested is zero, the behavior is implementation- defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object."
To maintain my sanity as a C programmer, I steer away from "implementation specific behavior" as much as possible, thus I prefer checking if buffer size is non-zero before allocating.
- zAy0LfpBZLC8mAC 11y agoI am not talking about C, but about the abstract concept. C's implementation is obviously kindof broken. Just as C would be broken if the behaviour of "zero additions" was somehow "implementation defined". That doesn't mean that something is fundamentally wrong with adding zero, it just means that you have to work around a broken language.
- kbart 11y agoThe topic is about C and I never said that such behavior is normal or anticipated. It's one of many cases where C standard lacks concreteness and we can only workaround such issues. In a perfect world, many things would be different.
- zAy0LfpBZLC8mAC 11y agoWell, then I might have misread your comment. I thought you meant that "it's just illogical to allocate 0 bytes anyway, just as it doesn't make sense to divide by 0".