The Wayback Machine - https://web.archive.org/web/20211008164318/https://en.cppreference.com/w/cpp/types/size_t
Namespaces
Variants
Views
Actions

std::size_t

From cppreference.com
< cpp‎ | types
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)

(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)

Elementary string conversions
(C++17)
(C++17)

Stacktrace
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
size_t
(C++17)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)

(C++11)(until C++20)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Constant evaluation context
Supported operations
Relationships and property queries
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)
(C++11)
(C++17)

(C++11)(until C++20)(C++17)
 
Defined in header <cstddef>
Defined in header <cstdio>
Defined in header <cstdlib>
Defined in header <cstring>
Defined in header <ctime>
Defined in header <cuchar>
(since C++17)
Defined in header <cwchar>
typedef /*implementation-defined*/ size_t;

std::size_t is the unsigned integer type of the result of the sizeof operator as well as the sizeof... operator and the alignof operator (since C++11).

The bit width of std::size_t is not less than 16.

(since C++11)

[edit] Notes

std::size_t can store the maximum size of a theoretically possible object of any type (including array). A type whose size cannot be represented by std::size_t is ill-formed (since C++14) On many platforms (an exception is systems with segmented addressing) std::size_t can safely store the value of any non-member pointer, in which case it is synonymous with std::uintptr_t.

std::size_t is commonly used for array indexing and loop counting. Programs that use other types, such as unsigned int, for array indexing may fail on, e.g. 64-bit systems when the index exceeds UINT_MAX or if it relies on 32-bit modular arithmetic.

When indexing C++ containers, such as std::string, std::vector, etc, the appropriate type is the member typedef size_type provided by such containers. It is usually defined as a synonym for std::size_t.

[edit] Example

#include <cstddef>
#include <iostream>
#include <array>
 
int main()
{
    std::array<std::size_t,10> a;
    std::size_t i{};
 
    for (i = 0; i != a.size(); ++i)
        std::cout << (a[i] = i) << ' ';
    std::cout << "│ i = " << i << '\n';
 
    for (i = a.size()-1; i < a.size(); --i)
        std::cout << a[i] << ' ';
    std::cout << "│ i = " << i << '\n';
}

Possible output:

0 1 2 3 4 5 6 7 8 9 │ i = 10
9 8 7 6 5 4 3 2 1 0 │ i = 18446744073709551615

[edit] See also

signed integer type returned when subtracting two pointers
(typedef) [edit]
byte offset from the beginning of a standard-layout type to specified member
(function macro) [edit]
integer literals decimal, octal, or hexadecimal numbers of integer type (C++11) [edit]
C documentation for size_t