简体   繁体   中英

Capacity of fundamental types across different platforms

I know that sizeof(type) will return different values, depending on the platform and the compiler.

However, I know that whenever talking about ints (int32) it is said that it can be one of 2^32 values.

If I'm on a platform where int32 is 8 bytes, it's theoretical maximum is 2^64. Can it really store that much data, or does it always store 4 bytes and use 4 bytes for padding?

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.

Particularly when dealing with files. If I write int32 to file, will it always store 4 bytes, or will it depend?

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). Right?

std::int32_t has always a size of 32bit (usually 4 bytes).

The size of int can vary and depends on the platform you compile for, but at least 16 bit (usually 2 bytes).

You can check the max value of your type in C++:

#include <limits>

std::numeric_limits<std::int32_t>::max()
std::numeric_limits<int>::max()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM