Are there any implementations that have size_t
defined as something else than unsigned int
? Under every system I worked, it was defined as unsigned int, so I'm just curious.
-
3I think the idea is that it's not *guaranteed* to be, thus being called `size_t`. It's a type expected by some specific C library calls to define a size argument (for example, the length of a buffer). I wouldn't write code that assumes it's an `unsigned int` even if you've never seen it defined otherwise. – lurker Nov 07 '15 at 16:40
-
1Please choose one of C and C++. – fuz Nov 07 '15 at 17:00
7 Answers
No. But it is an unsigned integer type. It need not be int specifically.
For C++
http://en.cppreference.com/w/cpp/types/size_t
C++ standard secton 18.2.6
The type
size_t
is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.
For linux; "Where is c++ size_t defined in linux" gives long unsigned int
For C
(The question was originally tagged both C and C++.)
have a look at the answers here:
What's sizeof(size_t) on 32-bit vs the various 64-bit data models?

- 1
- 1

- 26,184
- 12
- 71
- 97
x86-64 and aarch64 (arm64) Linux, OS X and iOS all have size_t
ultimately defined as unsigned long
. (This is the LP64 model. This kind of thing is part of the platform's ABI which also defines things like function calling convention, etc. Other architectures may vary.) Even 32-bit x86 and ARM architectures use unsigned long
on these OSes, although long
happens to be the same representation as an int
in those cases.
I'm fairly sure it's an unsigned __int64
/unsigned long long
on Win64. (which uses the LLP64 model)

- 22,018
- 3
- 52
- 103
There's absolutely no warranty that size_t
is defined as any "concrete" type. Actually, if you want to see a system where it's not unsigned int
, see, for instance, my machine (unsigned long
), and thus most 64-bit GNU/Linux systems.
From N4296, section 18.2.6:
The type
size_t
is an implementation-defined unsigned integer type that is large enough to contain the size bytes of any object.
Also, from N897 (Rationale) 6.5.3.4...
The type of
sizeof
, whatever it is, is published (in the library header<stddef.h>
) assize_t
, since it is useful for the programmer to be able to refer to this type. This requirement implicitly restrictssize_t
to be a synonym for an existing unsigned integer type. Note also that, althoughsize_t
is an unsigned type,sizeof
does not involve any arithmetic operations or conversions that would result in modulus behavior if the size is too large to represent as asize_t
, thus quashing any notion that the largest declarable object might be too big to span even with anunsigned long
in C89 oruintmax_t
in C9X. This also restricts the maximum number of elements that may be declared in an array, since for any arraya
ofN
elements,
N == sizeof(a)/sizeof(a[0])
Thus
size_t
is also a convenient type for array sizes, and is so used in several library functions.

- 8,248
- 2
- 19
- 41
No, it is unsigned integer type (not unsigned int
). From CPPReference:
std::size_t is the unsigned integer type of the result of the sizeof operator
But you can't say exactly what its size is, that I guess depends on the platform.

- 27,046
- 9
- 53
- 90
From the sys/types.h man page :
size_t shall be an unsigned integer type.
So at least theoretically it could be an unsigned short
, int
, long
, or long long
.

- 1
- 1

- 1,225
- 8
- 14
No. size_t
can and does differ from unsigned int
.
Per the C standard, 6.5.3.4:
The value of the result of both operators is implementation-defined, and its type (an unsigned integer type) is
size_t
, defined in<stddef.h>
(and other headers).
Under the C standard, size_t
is an undefined unsigned integer type.
Per the Open Group (POSIX, etc):
64-bit and Data Size Neutrality
...
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) = sizeof(size_t)
Simply put:
size_t
is size_t
. Code that fails to conform to that is wrong.

- 32,625
- 3
- 24
- 56