When is uint8_t ≠ unsigned char?(什么时候 uint8_t ≠ unsigned char?)
问题描述
根据 C 和 C++,CHAR_BIT >= 8
.
但是每当 CHAR_BIT >8
, uint8_t
甚至不能表示为 8 位.
它必须更大,因为 CHAR_BIT
是系统上任何数据类型的最小位数.
According to C and C++, CHAR_BIT >= 8
.
But whenever CHAR_BIT > 8
, uint8_t
can't even be represented as 8 bits.
It must be larger, because CHAR_BIT
is the minimum number of bits for any data type on the system.
在什么样的系统上,uint8_t
可以被合法地定义为 unsigned char
以外的类型?
On what kind of a system can uint8_t
be legally defined to be a type other than unsigned char
?
(如果 C 和 C++ 的答案不同,那么我想知道两者.)
(If the answer is different for C and C++ then I'd like to know both.)
推荐答案
如果存在,uint8_t
必须始终与 unsigned char
具有相同的宽度.但是,它不必是相同的类型;它可能是一个不同的扩展整数类型.它也不需要与 unsigned char
具有相同的表示;例如,可以以相反的顺序解释这些位.这是一个愚蠢的例子,但它对 int8_t
更有意义,其中 signed char
可能是补码或符号大小,而 int8_t
是必需的成为二进制补码.
If it exists, uint8_t
must always have the same width as unsigned char
. However, it need not be the same type; it may be a distinct extended integer type. It also need not have the same representation as unsigned char
; for instance, the bits could be interpreted in the opposite order. This is a silly example, but it makes more sense for int8_t
, where signed char
might be ones complement or sign-magnitude while int8_t
is required to be twos complement.
即使在普通"系统上为 uint8_t
使用非字符扩展整数类型的另一个优势"是 C 的别名规则.字符类型可以给任何东西做别名,这会阻止编译器大量优化同时使用字符指针和指向其他类型的指针的函数,除非 restrict
关键字应用得很好.但是,即使 uint8_t
具有与 unsigned char
完全相同的大小和表示形式,如果实现使其成为独特的非字符类型,则别名规则也不适用于例如,编译器可以假设 uint8_t
和 int
类型的对象永远不能别名.
One further "advantage" of using a non-char extended integer type for uint8_t
even on "normal" systems is C's aliasing rules. Character types are allowed to alias anything, which prevents the compiler from heavily optimizing functions that use both character pointers and pointers to other types, unless the restrict
keyword has been applied well. However, even if uint8_t
has the exact same size and representation as unsigned char
, if the implementation made it a distinct, non-character type, the aliasing rules would not apply to it, and the compiler could assume that objects of types uint8_t
and int
, for example, can never alias.
这篇关于什么时候 uint8_t ≠ unsigned char?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么时候 uint8_t ≠ unsigned char?
- 如何对自定义类的向量使用std::find()? 2022-11-07
- Stroustrup 的 Simple_window.h 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 近似搜索的工作原理 2021-01-01
- C++ 协变模板 2021-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 静态初始化顺序失败 2022-01-01