What's the difference between size_t and int in C++?

tunnuz picture tunnuz · Feb 2, 2009 · Viewed 130.1k times · Source

In several C++ examples I see a use of the type size_t where I would have used a simple int. What's the difference, and why size_t should be better?

Answer

Joao da Silva picture Joao da Silva · Feb 2, 2009

From the friendly Wikipedia:

The stdlib.h and stddef.h header files define a datatype called size_t which is used to represent the size of an object. Library functions that take sizes expect them to be of type size_t, and the sizeof operator evaluates to size_t.

The actual type of size_t is platform-dependent; a common mistake is to assume size_t is the same as unsigned int, which can lead to programming errors, particularly as 64-bit architectures become more prevalent.

Also, check Why size_t matters