3 ms·
I don't recall where, but I remember once encountering an API that took a buffer and a length, and required the buffer to exceed the specified length by some n.
by asveikau 6y ago
I don't recall where, but I remember once encountering an API that took a buffer and a length, and required the buffer to exceed the specified length by some n. Unlike this case, I think the requirement was documented. Still seemed like a recipe for bugs. If you need to implicitly adjust the length of the buffer upwards, it's not really the length of the buffer.
- Someone 6y agoRecipe for bugs but possibly also a recipe for performance. I’m not making any claims for this particular case, but if a function taking a large buffer to modify needs some extra space, and the code can get faster by storing that at the end of the buffer, requiring the caller to reserve that space can mean not having to copy the buffer twice, increasing memory use and execution time. A fairly common example is room for adding extra zero string terminators, so that an vectorized algorithm walking the input always hits one of them.
- asveikau 6y agoI think there are better ways to encode that in API design that are less ambiguous. For example perhaps having a function or some other means to compute required buffer size, in a way that makes such padding requirements relatively opaque to the caller.