size_t. A basic unsigned integer C/C++ type. It is the type of the result returned by sizeof operator. The type's size is chosen so that it could store the maximum size of a theoretically possible array of any type. On a 32-bit system size_t will take 32 bits and on a 64-bit one - 64 bits. In other words, a pointer can be safely put inside size_t type (an exception is class-function-pointers but this is a special case). size_t type is usually used for loop, array indexing, size storage and address arithmetic. Although size_t can store a pointer, it is better to use another unsinged integer type uintptr_t for that purpose (its name reflects its capability). In some cases using size_t type is more effective and safe than using a more habitual for the programmer unsigned type.
size_t is a base unsigned integer memsize-type defined in the standard library of C/C++ languages. This type is described in the header file stddef.h for C and in the file cstddef for C++. Types defined by the header file stddef.h are located in the global namespace while cstddef places the size_t type in the namespace std. Since the standard header file stddef.h of the C language is included into C++ programs for the purpose of compatibility, in these programs you may address the type both in the global namespace (::size_t, size_t) and namespace std (std::size_t).
In the terms of static analyzer PVS-Studio, type size_t refers to memsize-types. The analyzer includes Viva64 system for detailed error detection in 64-bit programs and for code optimization. Many diagnostic messages shown by Viva64 analyzer relate to recommendations on using memsize-types. Using memsize-types (such as size_t, ptrdiff_t, INT_PTR) instead of 32-bit types in 64-bit programs allows you to:
- enable the compiler to build a simpler and consequently faster code which will have no unnecessary conversions of 32-bit and 64-bit data. It is especially useful when operating with address arithmetic and array indexing;
- avoid some errors when processing a large size of input data when the number of the elements being processed excesses the number UINT_MAX;
- avoid some other more specific errors;
- make the code more portable among 64-bit versions of Windows and Linux systems which use different data models. Thus, for example, for indexing large arrays in Linux systems you can use unsigned long type while in Windows it is impossible.
To learn more about the errors you can avoid when using size_t type and also how this type allows improving and optimizing your 64-bit programs, see the articles given in the references.
If you are planning to start developing 64-bit projects or porting the existing 32-bit projects on 64-bit systems, we would like to offer you purchasing PVS-Studio analyzer which will simplify this task greatly and allow you to avoid the long period of searching hidden errors.
- About size_t and ptrdiff_t
- Optimization of 64-bit programs
- Seven Steps of Migrating a Program to a 64-bit System
- A 64-bit horse that can count
- 20 issues of porting C++ code on the 64-bit platform