Re: max size for printf() format conversion?
- From: Michael Mair <Michael.Mair@xxxxxxxxxxxxxxx>
- Date: Wed, 01 Jun 2005 23:10:54 +0200
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. .
- References:
- Re: max size for printf() format conversion?
- From: Keith Thompson
- Re: max size for printf() format conversion?
- Prev by Date: Re: "comma delimited" numeric output from printf()???
- Next by Date: Re: "comma delimited" numeric output from printf()???
- Previous by thread: Re: max size for printf() format conversion?
- Next by thread: Re: max size for printf() format conversion?
- Index(es):
Relevant Pages
|