c - 精确宽度整数类型的对象存储

标签 c standards-compliance stdint

如果存储在它们中的值符合需要的范围?

例如,考虑一个使用 9 位字节的 CPU。为这个 CPU 编写的实现可能有类似 typedef __uint8_as_9 uint8_t 的东西,假设 __uint8_as_9 指的是一个编译器关键字,指示一个 9 位无符号整数,它表示一个 8 位无符号整数.当编译器生成将 unsigned int 转换为该类型的代码时,它可能(隐含地)将数字截断为 9 位,然后屏蔽掉最高位。这是否仍被视为 uint8_t 的符合定义,因为编译器确保 9 位值在 0UINT8_MAX 范围内?

引用 C 标准 §7.20.1.1/3,关于精确宽度整数类型(强调我的):

These types are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two’s complement representation, it shall define the corresponding typedef names.

由于 uint8_t 的底层存储实际上由 9 位组成,因此这个最高有效位是否被视为填充位?即使它对使用此实现的任何(符合)程序完全透明?

最佳答案

用于具有 9 位字节(即 CHAR_BIT==9)的系统的编译器根本不会定义 uint8_tint8_t。如果确实如此,第 9 位将是一个填充位,这将违反标准的要求。

类型是可选的,无法满足其所有要求的实现不会提供这些类型。

这样的实现仍然会提供[u]int_least8_t[u]int_fast8_t,程序可以检查宏UINT8_MAX是否是定义来判断是否可以使用uint8_t

关于c - 精确宽度整数类型的对象存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25088938/

相关文章:

无法在 malloc() 处设置断点

XML/XSLT/Xpath 标准合规性

c++ - 在 Android NDK 上检测 stdint.h 和 C++11

java - Java 的 stdint.h?

visual-c++-6 - VC6 : fatal error C1083: Cannot open include file: 'stdint.h'

c - 在 C 中递归搜索目录中最大的文件

c - 每秒百万次循环(MCPS)是什么意思

c - struct数组的数据结构

c - 如何根据 C11 使用二进制前缀?

c++ - C++ 中的 "-->"运算符是什么?