4

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.

fuz
  • 88,405
  • 25
  • 200
  • 352
McLovin
  • 3,295
  • 7
  • 32
  • 67
  • 3
    I 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
  • 1
    Please choose one of C and C++. – fuz Nov 07 '15 at 17:00

7 Answers7

5

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?

Community
  • 1
  • 1
Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
5

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)

pmdj
  • 22,018
  • 3
  • 52
  • 103
3

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>) as size_t, since it is useful for the programmer to be able to refer to this type. This requirement implicitly restricts size_t to be a synonym for an existing unsigned integer type. Note also that, although size_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 a size_t, thus quashing any notion that the largest declarable object might be too big to span even with an unsigned long in C89 or uintmax_t in C9X. This also restricts the maximum number of elements that may be declared in an array, since for any array a of N 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.

3442
  • 8,248
  • 2
  • 19
  • 41
2

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.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
1

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.

Community
  • 1
  • 1
David Duncan
  • 1,225
  • 8
  • 14
1

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.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
0

No. I just ran this program on cpp.sh which is gcc:

#include <iostream>

int main()
{
  std::cout << sizeof(unsigned int) << " " << sizeof(unsigned long) << " " << sizeof(size_t) << "\n";
}

and it produce this output:

4 8 8

Anon Mail
  • 4,660
  • 1
  • 18
  • 21