c++ - uint32_t如何保证32位?

标签 c++ c int size int32

在大多数实现中,我看到 uint32_t 定义为

typedef unsigned int uint32_t;

但据我了解,intnot 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/

相关文章:

c - 我的用于从文本文件中删除行的 c 程序适用于小文件,但在处理大文件时出现 stacksmash 错误

c - C程序设计语言第二版练习1-9

c++ - 在 Arduino 上从字符串中提取整数

c++ - 是否可以在 C++ 中将模板与其参数分开?

c++ - FormatMessage 失败,错误代码为 317

c - TTF_RenderText 锁定

c - 是什么导致了这些默认数组值?

c - 定义接受 short int 而不是 int 的函数的好处

c++ - cc.game.restart() 在 cocos2d-js 3.10 扩展 sdkbox 时 carsh

c++ - cmake的困境