在大多数实现中,我看到 uint32_t
定义为
typedef unsigned int uint32_t;
但据我了解,int
是 not always guaranteed to be 4 bytes在所有系统中。那么如果系统有非 4 字节整数,uint32_t
如何保证 4?
最佳答案
需要一个实现来定义 uint32_t
正确(如果不可能,则根本不正确)。
如果unsigned int
满足要求(32位宽,无填充位),实现可以定义为
typedef unsigned int uint32_t;
如果没有,但是 unsigned long
确实满足要求,可以定义为:
typedef unsigned long uint32_t;
或者如果合适,它可以使用不同的无符号类型。
<stdint.h>
header 必须与它所使用的编译器兼容。如果你拿了<stdint.h>
无条件定义的 header uint32_t
作为unsigned int
, 并将其与生成 unsigned int
的编译器一起使用16 位,结果将是不合格的实现。
可以通过为编译器定制头文件或编写头文件使其适应编译器的特性来保持兼容性。
作为一名程序员,您不必担心如何保持正确性(当然好奇也没有错)。您可以相信它会正确完成——或者您可以向提供商投诉严重的错误。
关于c++ - uint32_t如何保证32位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51977116/