tags:

views:

6115

answers:

5

I notice that modern C and C++ code seems to use size_t instead of int/unsigned int pretty much everywhere - from parameters for C string functions to the STL. I am curious as to the reason for this and the benefits it brings.

+28  A: 

size_t is the unsigned integer type of the result of the sizeof operator (and of the offsetof operator) so it is guaranteed to be big enough to contain the size of the biggest object your system can handle (e.g. a static array of 8Gb).

It may be bigger, equal or even smaller than an unsigned int and your compiler might make assumption about it for optimization purpose.

You may find more precise information on the C99 standard (section 7.17), a draft of which is available on the Internet in pdf format.

Remo.D
"size of the biggest object", that's very poor wording. don't you mean "width of a pointer"? size_t is not 64bit, even on a 64bit CPU unless the operating system and memory address space is in 64bit mode.
Matt Joiner
+5  A: 

Type size_t must be big enough to store the size of any possible object. Unsigned int doesn't have to satisfy that condition.

For example in 64 bit systems int and unsigned int may be 32 bit wide, but size_t must be big enough to store numbers bigger than 4G

Maciej Hehl
poor wording, "object" is very vague, and it has entirely everything to do with memory addresses and pointers.
Matt Joiner
"object" is the language used by the standard.
R..
+10  A: 

Classic C (the early dialect of C described by Brian Kernighan and Dennis Ritchie in The C Programming Language, Prentice-Hall, 1978) didn't provide size_t. The C standards committee introduced size_t to eliminate a portability problem

Explained in detail at (with a very good example)

http://www.embedded.com/columns/programmingpointers/200900195

azeemarif
The link is so great! Thanks
Grissiom
+8  A: 

The size_t type is the type returned by the sizeof operator. It is an unsigned integer capable of expressing the size in bytes of any memory range supported on the host machine. It is (typically) related to ptrdiff_t in that ptrdiff_t is a signed integer value such that sizeof(ptrdiff_t) and sizeof(size_t) are equal.

When writing C code you should always use size_t whenever dealing with memory ranges.

The int type on the other hand is basically defined as the size of the (signed) integer value that the host machine can use to most efficiently perform integer arithmetic. For example, on many older PC type computers the value sizeof(size_t) would be 4 (bytes) but sizeof(int) would be 2 (byte). 16 bit arithmetic was faster than 32 bit arithmetic, though the CPU could handle a (logical) memory space of up to 4 GiB.

Use the int type only when you care about efficiency as its actual precision depends strongly on both compiler options and machine architecture. In particular the C standard specifies the following invariants: sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) placing no other limitations on the actual representation of the precision available to the programmer for each of these primitive types.

Note: This is NOT the same as in Java (which actually specifies the bit precision for each of the types 'char', 'byte', 'short', 'int' and 'long').

Kevin S.
+2  A: 

size_t is the size of a pointer.

So in 32 bits or the common ILP32 (integer, long, pointer) model size_t is 32 bits. and in 64 bits or the common LP64 (long, pointer) model size_t is 64 bits (integers are still 32 bits).

There are other models but these are the ones that g++ use (at least by default)