什么之间的区别" 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标准不适合的内部指定位数。它给出了一个范围[-32767,32767],它必须包含。
- 我知道在第一次大多数人会说,但是这意味着范围内至少有16位!但是C不要求补码加到恭维(甚至二进制)整数存储。如果他们说:16位,可能有一些平台,有1位奇偶校验位,1位标志,而14位的幅度,将仍然是达标,但不能满足该范围。
- 本标准不说关于 INT 是最有效的类型什么。除了上面的尺寸要求, INT 可以由编译器开发基于标准的任何他们认为最重要的决定。 (速度,大小,向后兼容性等)
- 在另一方面, int_fast16_t 就像是提供了一个提示,它应该使用一个类型,就是最佳的性能,可能在其他任何折衷为代价的编译器。
- 同样, int_least16_t 会告诉编译器使用这就是> = 16位,即使它会慢一些最小的类型。适合在大型阵列和东西preserving空间。
- 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 (or even binary) 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.
示例: MSVC上的x86-64有一个32位的内部,即使是在64位系统。 MS选择这样做,因为太多人认为的内部将始终是准确的32位,所以很多的ABI的将打破。然而,这是可能的, int_fast32_t 将是一个64位的号码,如果64位值分别为上x86-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 的该类型定义的宽度名称 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 nameuint_fastN_t
designates the fastest unsigned integer type with a width of at least N."