std::size_t - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::size_t

From cppreference.com
< cpp‎ | types

Defined in header `<cstddef>` |  |
Defined in header `<cstdio>` |  |
Defined in header `<cstring>` |  |
Defined in header `<ctime>` |  |
Defined in header `<cstdlib>` |  | (since C++11)
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).

### [edit] Notes

std::size_t can store the maximum size of a theoretically possible object of any type (including array). On many platforms (an exception are 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

Run this code

    #include <cstddef>

    int main()
    {
        const std::size_t N = 100;
        int* a = new int[N];

        for (std::size_t n = 0; n < N; ++n)
            a[n] = n;

        delete[] a;
    }

### [edit] See also

ptrdiff_t | signed integer type returned when subtracting two pointers (typedef) [edit]
offsetof | byte offset from the beginning of a standard-layout type to specified member (function macro) [edit]
C documentation for size_t

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/types/size_t&oldid=71842"