什么是 uint_fast32_t,为什么要使用它而不是常规的 int 和 uint32_t?

2021-12-25 00:00:00 c types c++

所以 typedef:ed 原始数据类型的原因是为了抽象低级表示并使其更容易理解(uint64_t 而不是 long long 类型,即 8 个字节).

So the reason for typedef:ed primitive data types is to abstract the low-level representation and make it easier to comprehend (uint64_t instead of long long type, which is 8 bytes).

但是,uint_fast32_tuint32_t 具有相同的 typedef.使用快速"版本会使程序更快吗?

However, there is uint_fast32_t which has the same typedef as uint32_t. Will using the "fast" version make the program faster?

推荐答案

  • int 在某些平台上可能小到 16 位.它可能不足以满足您的应用需求.
  • uint32_t 不保证存在.它是一个可选的 typedef,如果它具有恰好 32 位的无符号整数类型,则实现必须提供它.例如,有些具有 9 位字节,因此它们没有 uint32_t.
  • uint_fast32_t 清楚地说明了您的意图:这是一种至少 32 位的类型,从性能的角度来看是最好的.uint_fast32_t 实际上可能是 64 位长.这取决于实施.
    • int may be as small as 16 bits on some platforms. It may not be sufficient for your application.
    • uint32_t is not guaranteed to exist. It's an optional typedef that the implementation must provide iff it has an unsigned integer type of exactly 32-bits. Some have a 9-bit bytes for example, so they don't have a uint32_t.
    • uint_fast32_t states your intent clearly: it's a type of at least 32 bits which is the best from a performance point-of-view. uint_fast32_t may be in fact 64 bits long. It's up to the implementation.
    • ... 有 uint_fast32_tuint32_t 具有相同的 typedef ...

      ... there is uint_fast32_t which has the same typedef as uint32_t ...

      你看的不是标准.这是一个特定的实现(黑莓).所以你不能从那里推断出 uint_fast32_t 总是与 uint32_t 相同.

      What you are looking at is not the standard. It's a particular implementation (BlackBerry). So you can't deduce from there that uint_fast32_t is always the same as uint32_t.

      另见:

      • 标准委员会关心的异国架构.

      我对 C 和 C++ 中整数类型的基于意见的实用观点.

相关文章