std::size_t
is defined in any of:<cstddef>
<cstdio>
<cstdlib>
<cstring>
<ctime>
<cwchar>
Which is considered 'kosher' for getting just std::size_t
?
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.
The only time that using std::size_t is "better" than ::size_t is if you have not included <stddef. h> (perhaps you have #include <cstddef> instead).
Use size_t for variables that model size or index in an array. size_t conveys semantics: you immediately know it represents a size in bytes or an index, rather than just another integer. Also, using size_t to represent a size in bytes helps making the code portable. Save this answer.
If we consider the standard, both are integers of size 16 bits. On a typical 64-bit system, the size_t will be 64-bit, but unsigned int will be 32 bit. So we cannot use them interchangeably. One standard recommendation is that the size_t be at most as big as an unsigned long.
Since this is part of the C library I think the C standard specified header is the right one: stddef.h
, i.e. cstddef
.
From C11:
7.19 Common definitions
The header defines the following macros and declares the following types. Some are also defined in other headers, as noted in their respective subclauses.
[...]
size_t
which is the unsigned integer type of the result of the sizeof operator;
This is after all a C++ question so I think a quote from the C++ standard is more appropriate:
18.2 Types
Table 30 describes the header
<cstddef>
.Types:
ptrdiff_t
size_t
max_align_t
nullptr_t
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With