Where do I find the definition of size_t?
From Wikipedia
The
stdlib.h
andstddef.h
header files define a datatype calledsize_t
1 which is used to represent the size of an object. Library functions that take sizes expect them to be of typesize_t
, and the sizeof operator evaluates tosize_t
.The actual type of
size_t
is platform-dependent; a common mistake is to assumesize_t
is the same as unsigned int, which can lead to programming errors,2 particularly as 64-bit architectures become more prevalent.
From C99 7.17.1/2
The following types and macros are defined in the standard header
stddef.h
<snip>
size_t
which is the unsigned integer type of the result of the sizeof operator
Where is size_t Defined?
I presume this technically means that the definition of size_t in the global namespace has been deprecated?
Yes... but.
The Standard only mandates that std::size_t
must be defined1 by <cstddef>
, it does not disallow an implementation to define ::size_t
2, but if the implementation does, the two definitions must match3.
As a conclusion, you should use std::size_t
and should neither rely on ::size_t
to be defined nor define it.
The following are UB:
// DON'T
using size_t = std::size_t; // UB
using size_t = decltype(sizeof 1); // UB
1) [cstddef.syn]
namespace std {
using ptrdiff_t = see below;
using size_t = see below;
using max_align_t = see below;
using nullptr_t = decltype(nullptr);[...]
The contents and meaning of the header<cstddef>
are the same as the C standard library header<stddef.h>
, except that it does not declare the typewchar_t
, that it also declares the typebyte
and its associated operations ([support.types.byteops]
), and as noted in[support.types.nullptr]
and[support.types.layout]
.
2)[extern.types]/1
For each type
T
from the C standard library (These types are [...]size_t
,[...].), the types ::T
andstd::T
are reserved to the implementation[.]
3)[extern.types]/1
[...] when defined,
::T
shall be identical tostd::T
.
What is size_t in C?
From Wikipedia:
According to the 1999 ISO C standard
(C99),size_t
is an unsigned integer
type of at least 16 bit (see sections
7.17 and 7.18.3).
size_t
is an unsigned data type
defined by several C/C++ standards,
e.g. the C99 ISO/IEC 9899 standard,
that is defined instddef.h
.1 It can
be further imported by inclusion of
stdlib.h
as this file internally sub
includesstddef.h
.This type is used to represent the
size of an object. Library functions
that take or return sizes expect them
to be of type or have the return type
ofsize_t
. Further, the most
frequently used compiler-based
operator sizeof should evaluate to a
constant value that is compatible with
size_t
.
As an implication, size_t
is a type guaranteed to hold any array index.
What is size_t in C?
From Wikipedia:
According to the 1999 ISO C standard
(C99),size_t
is an unsigned integer
type of at least 16 bit (see sections
7.17 and 7.18.3).
size_t
is an unsigned data type
defined by several C/C++ standards,
e.g. the C99 ISO/IEC 9899 standard,
that is defined instddef.h
.1 It can
be further imported by inclusion of
stdlib.h
as this file internally sub
includesstddef.h
.This type is used to represent the
size of an object. Library functions
that take or return sizes expect them
to be of type or have the return type
ofsize_t
. Further, the most
frequently used compiler-based
operator sizeof should evaluate to a
constant value that is compatible with
size_t
.
As an implication, size_t
is a type guaranteed to hold any array index.
Where do I find the definition of size_t?
From Wikipedia
The
stdlib.h
andstddef.h
header files define a datatype calledsize_t
1 which is used to represent the size of an object. Library functions that take sizes expect them to be of typesize_t
, and the sizeof operator evaluates tosize_t
.The actual type of
size_t
is platform-dependent; a common mistake is to assumesize_t
is the same as unsigned int, which can lead to programming errors,2 particularly as 64-bit architectures become more prevalent.
From C99 7.17.1/2
The following types and macros are defined in the standard header
stddef.h
<snip>
size_t
which is the unsigned integer type of the result of the sizeof operator
Which header should I include for `size_t`?
Assuming I wanted to minimize the functions and types I was importing I'd go with cstddef
as it doesn't declare any functions and only declares 6 types. The others focus on particular domains (strings, time, IO) that may not matter to you.
Note that cstddef
only guarantees to define std::size_t
, that is, defining size_t
in namespace std
, although it may provide this name also in the global namespace (effectively, plain size_t
).
In contrast, stddef.h
(which is also a header available in C) guarantees to define size_t
in the global namespace, and may also provide std::size_t
.
What's the difference between size_t and int in C++?
From the friendly Wikipedia:
The stdlib.h and stddef.h header files define a datatype called size_t which is used to represent the size of an object. Library functions that take sizes expect them to be of type size_t, and the sizeof operator evaluates to size_t.
The actual type of size_t is platform-dependent; a common mistake is to assume size_t is the same as unsigned int, which can lead to programming errors, particularly as 64-bit architectures become more prevalent.
Also, check Why size_t matters
Related Topics
Inlining Failed in Call to Always_Inline '_M256D _Mm256_Broadcast_Sd(Const Double*)'
C/C++ Function Definitions Without Assembly
Are Virtual Destructors Inherited
How to Give Priority to Privileged Thread in Mutex Locking
C++ Efficiently Calculating a Running Median
Why Does the Most Negative Int Value Cause an Error About Ambiguous Function Overloads
What Happens When You Deallocate a Pointer Twice or More in C++
Why Does C++ Not Have a Const Constructor
Why Does a Std::Atomic Store with Sequential Consistency Use Xchg
C++ Expected Constant Expression
How to Store Functional Objects with Different Signatures in a Container
Opencv Multi Channel Element Access
How to Construct a Std::String from a Std::Vector<Char>
How to Write C++ Code Without Headers (Repetitive Function Declarations)