[英]Capacity of fundamental types across different platforms
I know that sizeof(type)
will return different values, depending on the platform and the compiler. 我知道sizeof(type)
将返回不同的值,具体取决于平台和编译器。
However, I know that whenever talking about ints (int32)
it is said that it can be one of 2^32 values. 但是,我知道,每当谈到ints (int32)
时,都可以说它可以是2 ^ 32值之一。
If I'm on a platform where int32
is 8 bytes, it's theoretical maximum is 2^64. 如果我在int32
为8字节的平台上,则理论上最大为2 ^ 64。 Can it really store that much data, or does it always store 4 bytes and use 4 bytes for padding? 它真的可以存储那么多数据,还是总是存储4个字节并使用4个字节进行填充?
The question really is, while I know that sizes of types will differ, I want to know whether asking for max_int on various platform will be constant or will it give me the value according to the type size. 问题确实是,虽然我知道类型的大小会有所不同,但我想知道在各种平台上请求max_int是否会保持不变,还是会根据类型大小为我提供值。
Particularly when dealing with files. 特别是在处理文件时。 If I write int32
to file, will it always store 4 bytes, or will it depend? 如果我将int32
写入文件,它将始终存储4个字节,还是取决于?
EDIT: Given all the comments and the fact that I'm trying to create an equivalent of C# BinaryReader, I think that using fixed size type is the best choice, since it would delegate all this to whoever uses it (making it more flexible). 编辑:考虑到所有评论以及我试图创建等效的C#BinaryReader的事实,我认为使用固定大小类型是最佳选择,因为它将所有这些都委派给使用它的人(使其更加灵活) 。 Right? 对?
std::int32_t has always a size of 32bit (usually 4 bytes). std :: int32_t的大小始终为32位(通常为4个字节)。
The size of int can vary and depends on the platform you compile for, but at least 16 bit (usually 2 bytes). int的大小可能会有所不同,并取决于要编译的平台,但至少为16位(通常为2个字节)。
You can check the max value of your type in C++: 您可以在C ++中检查类型的最大值:
#include <limits>
std::numeric_limits<std::int32_t>::max()
std::numeric_limits<int>::max()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.