[英]Can someone explain the significance of using uint32_t from the library <stdint.h>?
I recently saw someones code that was using this variable type and library on code chef. 我最近看到someones代码在代码厨师上使用这个变量类型和库。 I was wondering if someone can explain the benefits of using
uint32_t
as opposed to int
, float
, double
, etc. Also what cases should I use/not use it? 我想知道是否有人可以解释使用
uint32_t
而不是int
, float
, double
等的好处。还有什么情况我应该使用/不使用它?
Link to code: http://www.codechef.com/viewsolution/131898 链接到代码: http : //www.codechef.com/viewsolution/131898
The advantage is that a uint32_t
is always guaranteed to be 32 bits long, as opposed to the primitive types whose lengths are platform-dependent. 优点是
uint32_t
始终保证为32位长,而长度与平台相关的基本类型则相反。 For instance, while int
s are 32 bits on x86 and x86_64, they are 64 bits on many other 64-bit platforms, and less than that on some older and/or embedded architectures. 例如,虽然
int
在x86和x86_64上是32位,但在许多其他64位平台上它们是64位,而在某些旧的和/或嵌入式架构上则小于64位。
One of the cases where it may be beneficial to use a uint32_t
, then, could be when you read binary data directly to/from disk/network. 然后,当你直接从磁盘/网络读取二进制数据时,其中一个使用
uint32_t
可能是有益的情况。 You can always just copy 4 bytes into a uint32_t
and be sure that it fits. 您始终只需将4个字节复制到
uint32_t
并确保它适合。 (You'd still have to watch out for eg. differences in endianness, however.) (但是,您仍然需要注意例如字节序的差异。)
You may also want to use a uint32_t
if you simply want predictable overflow/underflow behavior. 如果您只是想要可预测的上溢/下溢行为,您可能还想使用
uint32_t
。 Or if you're doing calculations defined in some specific size, like when running some hashing algorithms. 或者,如果您正在进行某些特定大小的计算,例如运行某些散列算法时。
The only thing I have been left wondering is why there aren't corresponding float32_t
and float64_t
types. 我唯一想知道的是为什么没有相应的
float32_t
和float64_t
类型。 :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.