[英]What is the difference between an Uint32 and an unsigned int in C++?
Is it the same? 它是一样的吗? If no: what is the difference?
如果不是:有什么区别? If yes then why do you need this type?
如果是,那你为什么需要这种类型?
uint32_t
(or however pre-C++11 compilers call it) is guaranteed to be a 32-bit unsigned integer; uint32_t
(或者前C ++ 11编译器调用它)保证是32位无符号整数; unsigned int
is whatever unsigned integer the compiler likes best to call unsigned int
, as far as it meets the requirements of the standard (which demands for it a 0-65535 minimum range). unsigned int
是编译器最喜欢调用unsigned int
,只要它符合标准的要求(要求它为0-65535最小范围)。
Like int
, unsigned int
typically is an integer that is fast to manipulate for the current architecture (normally it fits into a register), so it's to be used when a "normal", fast integer is required. 与
int
类似, unsigned int
通常是一个快速操作当前体系结构的整数(通常它适合于寄存器),因此当需要“正常”快速整数时,它将被使用。
uint32_t
, instead, is used when you need an exact-width integer, eg to serialize to file, or when you require that exact range or you rely on unsigned overflow to happen exactly at 2^32-1
. 相反,
uint32_t
用于需要精确宽度整数时,例如序列化为文件,或者当您需要精确范围或依赖无符号溢出恰好发生在2^32-1
。
For example, on a 16 bit-processor unsigned int
will typically be 16 bits wide, while uint32_t
will have to be 32 bits wide. 例如,在16位处理器上,
unsigned int
通常为16位宽,而uint32_t
必须为32位宽。
Incidentally, as pointed out by @Marc Glisse , while unsigned int
is always present, uint32_t
is not mandatory - a particular compiler implementation may not provide it. 顺便提一下,正如@Marc Glisse所指出的,虽然
unsigned int
始终存在,但uint32_t
不是必需的 - 特定的编译器实现可能不会提供它。 This is mostly because not all platforms can easily provide such a type (typically DSPs with weird word sizes). 这主要是因为并非所有平台都能轻松提供这种类型(通常是具有奇怪字长的DSP)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.