What are the differences between size_t
and std::size_t
in terms of where they are declared, when they should be used and any other differentiating features?
C++ – Difference Between size_t and std::size_t
c++size-t
c++size-t
What are the differences between size_t
and std::size_t
in terms of where they are declared, when they should be used and any other differentiating features?
Best Answer
C's
size_t
and C++'sstd::size_t
are both same.In C, it's defined in
<stddef.h>
and in C++, its defined in<cstddef>
whose contents are the same as C header (see the quotation below). Its defined as unsigned integer type of the result of the sizeof operator.C Standard says in §17.7/2,
And C++ Standard says (about
cstddef
header) in §18.1/3,So yeah, both are same; the only difference is that C++ defines
size_t
instd
namespace.Please also notice that the above line also says "with the following changes" which isn't referring to
size_t
. Its rather referring to the new additions (mostly) made by C++ into the language (not present in C) which are also defined in the same header.Wikipedia has very good info about range and storage size of size_t:
And the rest you can read from this page at wikipedia.