Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top

size_t

06 Fév 2023

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 and alignof 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:

  • 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;
  • Since the standard guarantees that size_t can store the size of the largest possible object in the system, this type is used to store object sizes;
  • 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;
  • Because size_t can usually safely store a pointer, it is used for address arithmetic. Although it is better to use another unsinged integer type uintptr_t for that purpose — its name reflects its capability.
  • 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 static analyzer, the size_t type refers to memsize types. The analyzer has a large number of special 64-bit diagnostic rules 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 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

Popular related articles

S'abonner

Comments (0)

close comment form
close form

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam