﻿# size\_t

_size\_t_ is a special unsigned integer type defined in the standard library of C and C\+\+ languages\. It is the type of the result returned by the [_sizeof_](https://timsong-cpp.github.io/cppwp/n4861/expr.sizeof#5) and [_alignof_](https://timsong-cpp.github.io/cppwp/n4861/expr.alignof#2) operators\.

The _size\_t_ is chosen so that it can store the maximum size of a theoretically possible array or an object\. In other words, the number of bits in _size\_t_ is equal to the number of bits required to store the maximum address in the machine's memory\. On a 32\-bit system _size\_t_ will take 32 bits, on a 64\-bit one 64 bits\. It means a variable of _size\_t_ type can safely store a pointer\. The exceptions are platforms with memory segmentation and pointers to class functions\. 

This type shifts responsibility about the possible different behavior of integer variables when changing the platform from the programmer shoulders to the implementation of the standard library\. Therefore, using the _size\_t_ type is safer and more efficient than using ordinary unsigned integer types:

1. This type allows you to write loops and counters without worrying about possible overflow when changing platforms\. For example, when the number of required iterations exceeds _UINT\_MAX_;
1. Since the [standard](https://timsong-cpp.github.io/cppwp/n4861/support.types.layout#3) guarantees that _size\_t_ can store the size of the largest possible object in the system, this type is used to store object sizes;
1. Because of this same _size\_t_ characteristic, it is also used to index arrays\. Unlike the usual basic integer types, _size\_t_ guarantees that the index value cannot be greater than _SIZE\_MAX_;
1. Because _size\_t_ can usually safely store a pointer, it is used for [address arithmetic](https://pvs-studio.com/en/blog/terms/0005/)\. Although it is better to use another unsinged integer type [_uintptr\_t_](https://pvs-studio.com/en/blog/terms/0050/) for that purpose — its name reflects its capability\.
1. The compiler can build simpler and, therefore, faster code with no unnecessary conversions of 32\-bit and 64\-bit data\.

In C, the _size\_t_ type is declared in the following header files: _<stddef\.h\>_, _<stdlib\.h\>_, _<string\.h\>_, _<wchar\.h\>_, _<uchar\.h\>_, _<time\.h\>_, and _<stdio\.h\>_\. In C\+\+, the _size\_t_ type declaration is located in the files: <cstddef\>, _<cstdlib\>_, _<cstring\>_, _<cwchar\>_, _<cuchar\>_, _<ctime\>_, and _<cstdio\>_\. The _size\_t_ type is placed in the global namespace and in _std_\. Standard header files of the C language for backward compatibility can also be included in C\+\+ programs\.

In terminology of the [PVS\-Studio](https://pvs-studio.com/en/pvs-studio/) static analyzer, the _size\_t_ type refers to [memsize](https://pvs-studio.com/en/blog/terms/0030/) types\. The analyzer has a large number of special 64\-bit [diagnostic rules](https://pvs-studio.com/en/docs/warnings/#64CPP) issued by the analyzer and related to recommendations on using memsize\-types\. If you are planning to start developing cross\-platform projects or porting existing 32\-bit projects to 64\-bit systems, you may use [PVS\-Studio](https://pvs-studio.com/en/pvs-studio/) analyzer which can simplify this task greatly and allow you to avoid the long stage of searching for hidden errors\.

To learn more about the errors you can avoid when using _size\_t_ type and also how this type allows improving and optimizing your programs, see the articles given in the references\.

## References

1. [About size\_t and ptrdiff\_t](https://pvs-studio.com/en/blog/posts/cpp/a0050/)\.
1. [Optimization of 64\-bit programs](https://pvs-studio.com/en/blog/posts/cpp/a0030/)\.
1. [Seven steps of migrating a program to a 64\-bit system\.](https://pvs-studio.com/en/blog/posts/cpp/a0042/)
1. [A 64\-bit horse that can count](https://pvs-studio.com/en/blog/posts/cpp/a0043/)\.
1. [Lessons on development of 64\-bit C and C\+\+ applications](https://pvs-studio.com/en/blog/lessons/full/)\.