uint32 类型定义的最佳实践



在 long 和 int 都是 4 个字节的系统上,哪个是最好的,为什么?

typedef unsigned long u32;

typedef unsigned int u32;

注意:uint32_t不是一种选择

如今,

每个平台都有定义uint32_t stdint.h或其C++等效cstdint。 请使用标准类型,而不是创建自己的类型。

  • http://pubs.opengroup.org/onlinepubs/7999959899/basedefs/stdint.h.html
  • http://www.cplusplus.com/reference/cstdint/
  • http://msdn.microsoft.com/en-us/library/hh874765.aspx

两者之间的大小相同,因此仅取决于您的使用情况。如果需要存储十进制值,请使用 long。

这里有一个更好和完整的答案:https://stackoverflow.com/questions/271076/what-is-the-difference-between-an-int-and-a-long-in-c/271132

编辑:我不确定十进制和长,如果有人可以确认,谢谢。

既然

你说标准uint32_t不是一个选项,那么在32位机器上使用longint都是正确的,我会说

typedef unsigned int u32;

稍微好一点,因为在两种流行的 64 位机器数据模型(LLP64 和 LP64)上,int仍然是 32 位,而long可能是 32 位或 64 位。查看 64 位数据模型

最新更新