4 ms·
I don't get it. For me it's always a simple logic: if you didn't have anything (0 bytes) to allocate/reallocate you simply don't do that. Period. It's like divi
by kbart 11y ago
I don't get it. For me it's always a simple logic: if you didn't have anything (0 bytes) to allocate/reallocate you simply don't do that. Period. It's like division by zero -- you always have to check if divisor is not equal to zero; if it was -- skip division altogether.
- zAy0LfpBZLC8mAC 11y agoExcept the semantics of a zero-length buffer are perfectly well-defined, while the semantics of division by zero are not defined at all in the types commonly used in computing. A proper comparison therefore would be addition, maybe, which is also perfectly well-defined for zero operands. Do you special-case "zero addition" in your code? if(a==0){if(b==0){x=0;}else{x=b;}}else{if(b==0){x=a;}else{x=a+b;}} ?
- kbart 11y agoI'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".
- klodolph 11y agoZero-length objects in general may behave quite differently, because there may or may not be a guarantee that pointers to two different zero-length objects compare as unequal. In particular, this is a point where GCC's C and C++ implementations differ!
- aardvark179 11y agoExcept that the interactions with the rest of the standard become very bad. E.g. memcpy to or from NULL, even for zero bytes, is undefined and that will be taken advantage of by your compiler in ways you really didn't expect.
- revelation 11y agoBecause checking for this case actually makes the logic in every piece of code more difficult, since you now have to account for the edge case. This is superfluous, because in the edge case nothing bad was going to happen, anyway; a correct program wouldn't ever dereference the return value, and an incorrect program is incorrect regardless of the behavior of malloc here.