“int"和“int"有什么区别?和“int_fast16_t"? [英] What's the difference between "int" and "int_fast16_t"?

查看:68
本文介绍了“int"和“int"有什么区别?和“int_fast16_t"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,C 规范说 int 类型应该是目标平台上最有效的类型,至少包含 16 位.

As I understand it, the C specification says that type int is supposed to be the most efficient type on target platform that contains at least 16 bits.

int_fast16_t 的 C99 定义不也是这样吗?

Isn't that exactly what the C99 definition of int_fast16_t is too?

也许他们把它放在那里只是为了保持一致性,因为需要其他 int_fastXX_t?

Maybe they put it in there just for consistency, since the other int_fastXX_t are needed?

更新

总结以下讨论:

  • 我的问题在很多方面都是错误的.C 标准没有为 int 指定位数.它给出了它必须包含的范围 [-32767,32767].
  • 我意识到起初大多数人会说,但该范围意味着至少 16 位!"但是 C 不需要整数的二进制补码存储.如果他们说16 位",则可能存在一些具有 1 位奇偶校验、1 位符号和 14 位幅度的平台仍然符合标准",但不满足该范围.
  • 标准没有说明 int 是最有效的类型.除了上述大小要求之外,int 可以由编译器开发人员根据他们认为最重要的任何标准来决定.(速度、大小、向后兼容性等)
  • 另一方面,int_fast16_t 就像向编译器提供了一个提示,即它应该使用对性能而言最优化的类型,但可能会以任何其他折衷为代价.
  • 同样,int_least16_t 会告诉编译器使用 >= 16 位的最小类型,即使它会更慢.适合在大型数组和其他东西中保留空间.
  • My question was wrong in many ways. The C standard does not specify bitness for int. It gives a range [-32767,32767] that it must contain.
  • I realize at first most people would say, "but that range implies at least 16-bits!" But C doesn't require two's-compliment storage of integers. If they had said "16-bit", there may be some platforms that have 1-bit parity, 1-bit sign, and 14-bit magnitude that would still being "meeting the standard", but not satisfy that range.
  • The standard does not say anything about int being the most efficient type. Aside from size requirements above, int can be decided by the compiler developer based on whatever criteria they deem most important. (speed, size, backward compatibility, etc)
  • On the other hand, int_fast16_t is like providing a hint to the compiler that it should use a type that is optimum for performance, possibly at the expense of any other tradeoff.
  • Likewise, int_least16_t would tell the compiler to use the smallest type that's >= 16-bits, even if it would be slower. Good for preserving space in large arrays and stuff.

示例: x86-64 上的 MSVC 具有 32 位 int,即使在 64 位系统上也是如此.MS 选择这样做是因为太多人认为 int 始终是 32 位的,因此很多 ABI 会损坏.但是,如果 64 位值在 x86-64 上更快,则 int_fast32_t 可能是 64 位数字.(我认为实际情况并非如此,但这只是说明了这一点)

Example: MSVC on x86-64 has a 32-bit int, even on 64-bit systems. MS chose to do this because too many people assumed int would always be exactly 32-bits, and so a lot of ABIs would break. However, it's possible that int_fast32_t would be a 64-bit number if 64-bit values were faster on x86-64. (Which I don't think is actually the case, but it just demonstrates the point)

推荐答案

int_fast16_t 保证是最快的 int,大小至少为 16 位.int 不保证它的大小,除了:

int_fast16_t is guaranteed to be the fastest int with a size of at least 16 bits. int has no guarantee of its size except that:

 sizeof(char) = 1 and sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long).

并且它可以容纳 -32767 到 +32767 的范围.

And that it can hold the range of -32767 to +32767.

(7.20.1.3p2) "typedef 名称 int_fastN_t 指定最快的有符号整数类型,宽度至少为 N.typedef 名称 uint_fastN_t 指定最快的无符号整数类型,宽度至少为 N."

(7.20.1.3p2) "The typedef name int_fastN_t designates the fastest signed integer type with a width of at least N. The typedef name uint_fastN_t designates the fastest unsigned integer type with a width of at least N."

这篇关于“int"和“int"有什么区别?和“int_fast16_t"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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