News for hackers. Stuff that matters.
Re:Once upon a time, memory allocators made sense by kbart on Monday January 18, 2016 @07:37AM (#10924031)
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.
Re:Once upon a time, memory allocators made sense by zAy0LfpBZLC8mAC on Monday January 18, 2016 @08:18AM (#10924186)
Except 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;}} ?
Re:Once upon a time, memory allocators made sense by kbart on Monday January 18, 2016 @09:05AM (#10924404)
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.

Re:Once upon a time, memory allocators made sense by zAy0LfpBZLC8mAC on Monday January 18, 2016 @09:20AM (#10924486)
I 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.
Re:Once upon a time, memory allocators made sense by kbart on Monday January 18, 2016 @10:13AM (#10924803)
The 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.
Re:Once upon a time, memory allocators made sense by zAy0LfpBZLC8mAC on Monday January 18, 2016 @10:52AM (#10925041)
Well, 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".
Re:Once upon a time, memory allocators made sense by klodolph on Monday January 18, 2016 @01:34PM (#10925975)
Zero-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!
Re:Once upon a time, memory allocators made sense by aardvark179 on Monday January 18, 2016 @09:19AM (#10924480)
Except 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.
Re:Once upon a time, memory allocators made sense by revelation on Monday January 18, 2016 @03:38PM (#10926746)
Because 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.