Why is std :: size_t 4 bytes on 32-bit systems when the unsigned long long is 8 bytes on 32-bit and 64-bit systems?

The question is pretty simple.

On 32-bit systems:

std::cout << sizeof(unsigned int); //4 std::cout << sizeof(unsigned long long); //8 std::cout << sizeof(std::size_t); //4 

On 64-bit systems:

 std::cout << sizeof(unsigned int); //4 std::cout << sizeof(unsigned long long); //8 std::cout << sizeof(std::size_t); //8 

I just checked the implementation that MSVC has and looks like this:

 #ifdef _WIN64 typedef unsigned __int64 size_t; #else typedef unsigned int size_t; #endif 

So, why not make std::size_t unsigned long long ( std::uintmax_t ) on both 32-bit and 64-bit systems when they explicitly support it? Or am I wrong about that?

+6
source share
2 answers

The point size_t must contain the size of the largest possible object. In a 32-bit system, no object can occupy more than 2 ** 32 bytes, so a 32-bit type is sufficient.

Using a 64-bit type will be wasteful space and potentially more expensive at runtime.

+8
source

It will be a waste. On a 32-bit machine, you have a 4 GB address space, so you cannot have objects larger than 4 GB, so the 32 bit range of size_t is quite adequate.

+6
source

All Articles