On a 64-bit system, sizeof(unsigned long)
depends on the data model implemented by the system, for example, it is 4 bytes on LLP64 (Windows), 8 bytes on LP64 (Linux, etc.). What's sizeof(size_t)
supposed to be? Does it vary with data model like sizeof(long)
does? If so, how?
References:
it should vary with the architecture because it represents the size of any object. So on a 32-bit system
size_t
will likely be at least 32-bits wide. On a 64-bit system it will likely be at least 64-bit wide.size_t is 64 bit normally on 64 bit machine
size_t is defined by the C standard to be the unsigned integer return type of the sizeof operator (C99 6.3.5.4.4), and the argument of malloc and friends (C99 7.20.3.3 etc). The actual range is set such that the maximum (SIZE_MAX) is at least 65535 (C99 7.18.3.2).
However, this doesn't let us determine sizeof(size_t). The implementation is free to use any representation it likes for size_t - so there is no upper bound on size - and the implementation is also free to define a byte as 16-bits, in which case size_t can be equivalent to unsigned char.
Putting that aside, however, in general you'll have 32-bit size_t on 32-bit programs, and 64-bit on 64-bit programs, regardless of the data model. Generally the data model only affects static data; for example, in GCC:
You'll note that pointers are 64-bit in all cases; and there's little point to having 64-bit pointers but not 64-bit sizes, after all.
EDIT: Thanks for the comments - I looked it up in the C99 standard, which says in section 6.5.3.4:
So, the size of
size_t
is not specified, only that it has to be an unsigned integer type. However, an interesting specification can be found in chapter 7.18.3 of the standard:Which basically means that, irrespective of the size of
size_t
, the allowed value range is from 0-65535, the rest is implementation dependent.