|
Boost : |
From: Maxim Yegorushkin (e-maxim_at_[hidden])
Date: 2004-10-26 07:42:25
Ben Hutchings <ben.hutchings_at_[hidden]> wrote:
[]
>> This is one of the reasons I find the *snprintf functions to be pure
>> evil. They don't return -1 when the output would be too long, they
>> return the size they WOULD have written if the buffer was large
>> enough.
>
> That's not evil; it's extremely useful if you want to dynamically
> allocate a large enough buffer. All you have to do is check that the
> return value is less than or equal to the length you passed in.
Man pages on my RH 9 AS say that a return value that is *equal* or greater than the size of the buffer means that the buffer is too small. That makes perfect sense because the function returns the number of characters written not including the null terminator, so if it returns a value equal to the size of the buffer than it did not have enough space for the null terminator, though the actual output in the buffer is null terminated.
> When built with Visual C++, it probably is. MS documents this function
> as returning a negative value in case of error. A portable test would
> be size_t(n) > sizeof(buf).
Shouldn't it be
size_t(n) >= sizeof(buf)
?
-- Maxim Yegorushkin
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk