Re: max size for printf() format conversion?



Keith Thompson wrote:
Michael Mair <Michael.Mair@xxxxxxxxxxxxxxx> writes:

Matt Garman wrote:

Is there a clean, portable way to determine the maximum value of
converted numerical fields with printf()-like functions?  Doing this
at compile-time would be preferable.
For example, %i should never convert to a string that is longer than
the number of digits in INT_MAX, right?
I'd like to create a structure that contains character
representations of numerical types (e.g., instead of storing 3421 as
an int, it would store the char array "3421").  I'd like to make
these fields a fixed-size static array, but obviously I don't want
to make them too small.

C89: The draft I use states that a single conversion can produce a maximum of at least 509 characters. C99 (standard): This limit goes up to 4095 characters.

Right, but I don't think that's what he's looking for. A C99 implementation isn't required to limit printf output to 4095 characters, so you can't assume that

    char buf[4096];
    sprintf(buf, some_format, arg1, arg2, arg3);

won't overflow the buffer.

Yep. Nonetheless, I think these environmental limits provide a lower bound for or at least give an impression of sensible buffer sizes. As it is, I would use the respective buffer size in conjunction with some strategy for error handling, i.e. if the static buffer is not enough and a large enough dynamic buffer cannot be allocated, one can try splitting the format string and at least get out the minimum maximum number of characters per conversion...

A function that would take a format string and return the maximum
possible length of the output it can generate would solve the problem.
If the format string contains "%s", of course, the result is limited
only by the possible length of a string.  The limit for "%d" would
typically be 6 on a system with 16-bit int, 11 on a system with 32-bit
int.  Floating-point formats are a bit more challenging.  The limit
for "%p" is difficult or impossible to determine without intimate
knowledge of the system.

One trick mentioned here recently is to use fprintf() to print to
/dev/null (or an equivalent data sink) and check the result.  This
gives you the length for a given set of arguments, not the maximum for
the format, but substituting INT_MIN for "%d" and so forth might give
you the answers you're looking for.  One drawback is that this
performs output (at least virtually), so the performance might not be
acceptable.

Well, on systems with such a "data sink" you also may have snprintf() in the library, which IMO is a more obvious candidate for a solution. Of course, one always should wrap a potentially not portable "character count" solution appropriately...


Cheers Michael -- E-Mail: Mine is an /at/ gmx /dot/ de address. .



Relevant Pages

  • Re: Delphi Quiz: SetLength( WideString, 10 );
    ... >> I call a function and the function returns a buffer of bytes. ... Let's assume it's a 16 bit unicode string. ... characters to a wide character encoding scheme such as Unicode. ...
    (alt.comp.lang.borland-delphi)
  • Re: Why crash ?
    ... Yes, but naively, I would assume that the optimal buffer size ... depended on a certain number of characters, ... small string optimization is that the std::string object has a certain ... If you allocate dynamic storage, the string object has to store ...
    (microsoft.public.vc.language)
  • Re: Why crash ?
    ... Yes, but naively, I would assume that the optimal buffer size ... depended on a certain number of characters, ... small string optimization is that the std::string object has a certain ... If you allocate dynamic storage, the string object has to store ...
    (microsoft.public.vc.language)
  • Re: Search a binary file for a string... again! (its to slow)
    ... >>Im writing a program to search for a string in a binary file. ... > Most of the time the first character in the buffer won't match the first ... characters, then taking 3 steps back, and reading ...
    (comp.lang.c)
  • [PATCH 1/5] add binary printf
    ... read from binary buffer for args and format a string ... Arguments for the format string ...
    (Linux-Kernel)