printf()格式转换的最大大小? [英] max size for printf() format conversion?

查看:91
本文介绍了printf()格式转换的最大大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种干净,便携的方式来确定

转换数字字段的最大值与printf() - 类似的函数?在编译时这样做
会更好。


例如,%i永远不应该转换为长于
$的字符串b $ b INT_MAX中的位数,对吗?


我想创建一个包含字符的结构

表示数字类型(例如而不是将3421存储为int,它将存储char数组3421。我想把

这些字段固定大小的静态数组,但显然我不想让它们变得太小。



谢谢!

马特


-

马特加曼

发送电子邮件至: http://raw-sewage.net/index。 php?file = email

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.

Thanks!
Matt

--
Matt Garman
email at: http://raw-sewage.net/index.php?file=email

推荐答案

Matt Garman< fa ** @ not-real.bogus>写道:
Matt Garman <fa**@not-real.bogus> wrote:
有没有一个干净,可移植的方法来确定
转换数字字段的最大值与printf() - 类似的功能?在编译时这样做会更好。
例如,%i永远不应该转换为比INT_MAX中的位数长的字符串,对吗?


两点:INT_MAX和INT_MIN可能相差多于一个,所以

在保存方面你必须考虑到这两点如果

您使用这些限制的值。对于负值,你需要为减号添加一个。

我想创建一个包含字符
数字表示的结构类型(例如,代替将3421存储为int,它将存储char数组3421)。我想将这些字段设为固定大小的静态数组,但显然我不想让它们太小。
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?
Two points: INT_MAX and INT_MIN could differ by more than one, so
to be on the save side you would have to take into account both if
you use the values of these limits. And for negative values you
would have to add one for the minus sign.
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.




如果我的计算没有错,你应该安全地使用


#defined MAX_INT_LENGTH((sizeof(int)* CHAR_BIT + 1)/ 3 + 1 )


(对于有符号和无符号的整数,你可以删除最后的+ 1以获得

无符号整数)。这可以在编译时进行评估,应该是

便携式。

问候,Jens

-

\\ \\ Jens Thoms Toerring ___ Je***********@physik.fu -berlin.de

\ __________________________ http:// www .toerring.de


Matt Garman写道:
Matt Garman wrote:
是否有一种干净,便携的方式来确定转换数字字段与printf() - 类似的函数?在编译时这样做会更好。

例如,%i永远不应该转换为比INT_MAX中的位数长的字符串,对吗?

我想创建一个包含数字类型的字符表示的结构(例如,不是将3421存储为int,而是存储char数组) ; 3421")。我想将这些字段设为固定大小的静态数组,但显然我不想让它们太小。
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:我使用的草案表明单次转换可以产生最多至少509个字符的



C99(标准):此限制上升
如果有疑问,请使用snprintf()/ vsnprintf()(如果可用)(和malloc())。


干杯

Michael

-

电子邮箱:我的是/ at / gmx / dot / de地址。



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.

If in doubt, use snprintf()/vsnprintf() if available (and malloc()).

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


Michael Mair< Mi ********** @ invalid.invalid>写道:
Michael Mair <Mi**********@invalid.invalid> writes:
Matt Garman写道:
Matt Garman wrote:
是否有一种干净,可移植的方法来确定
转换数字字段的最大值与printf()一样的函数?在编译时这样做
会更好。
例如,%i永远不应该转换为比INT_MAX中的位数长的字符串,对吗?
我想创建一个包含数字类型的字符表示的结构(例如,不是将3421存储为int,而是存储char数组3421)。我想将这些字段设为固定大小的静态数组,但显然我不想让它们太小。
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:我使用的草案表明单个转换最多可以生成至少509个字符。
C99(标准):此限制最多可达4095个字符。



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.




对,但我不认为那是他正在寻找的东西。一个C99

的实现并不需要将printf输出限制为4095

个字符,所以你不能认为


char buf [4096];

sprintf(buf,some_format,arg1,arg2,arg3);


不会溢出缓冲区。


一个函数可以获取一个格式字符串并返回它可以生成的输出的最大长度

可以解决问题。

如果格式字符串包含%s,当然,结果仅限于字符串的可能长度
。 %d的限制为%d。在具有16位int的系统上,
通常为6,在具有32位的系统上为11 b / b
。浮点格式更具挑战性。 %p的限制

很难或不可能确定没有亲密的

系统知识。


最近提到的一个技巧是使用fprintf()打印到

/ dev / null(或等效的数据接收器)并检查结果。这个

给出了给定参数集的长度,而不是格式为

的最大值,而是用INT_MIN代替%d。等你可能会给你想要的答案。

一个缺点是,这个

执行输出(至少是虚拟的),所以性能可能不是可接受的



- -

Keith Thompson(The_Other_Keith) ks***@mib.org < http:// www .ghoti.net / ~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



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.

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于printf()格式转换的最大大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆