size_t和std::size_t的区别。

时间:2021-08-27 19:30:16

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?

size_t和std::size_t之间的区别是什么?它们在什么地方被声明,什么时候应该被使用,以及其他的区别特征?

3 个解决方案

#1


66  

C's size_t and C++'s std::size_t are both same.

C的size_t和c++的std::size_t都是一样的。

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中,它在 和c++中,其定义在 中,其内容与C头相同(参见下面的引用)。它的定义为无符号整数类型,是sizeof运算符的结果。 中定义。h>

C Standard says in §17.7/2,

C标准在第17.7/2节,

size_t which is the unsigned integer type of the result of the sizeof operator

size_t,它是sizeof运算符结果的无符号整数类型。

And C++ Standard says (about cstddef header) in §18.1/3,

C++标准表示(关于cstddef头)

The contents are the same as the Standard C library header , with the following changes.

内容与标准C库标题相同,并有以下更改。

So yeah, both are same; the only difference is that C++ defines size_t in std namespace.

所以,两者都是一样的;唯一的区别是,c++定义了std名称空间中的size_t。

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.

请注意,上面的行还说了“with the following changes”,这并不是指size_t。它更倾向于将c++(大部分)添加到语言中(不存在于C中),而在相同的标题中也定义了这些内容。


Wikipedia has very good info about range and storage size of size_t:

Wikipedia对size_t的范围和存储大小有非常好的信息:

Range and storage size of size_t

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,[3][4] when moving from 32 to 64-bit architecture, for example.

实际类型的size_t是平台相关的;一个常见的错误是假设size_t与unsigned int相同,这可能导致编程错误,[3][4]在从32位架构迁移到64位架构时,例如。

According to the 1999 ISO C standard (C99), size_t is an unsigned integer type of at least 16 bits.

根据1999年ISO C标准(C99), size_t是一个无符号整数类型,至少16位。

And the rest you can read from this page at wikipedia.

剩下的你可以在*上看到。

#2


15  

From C++03 "17.4.3.1.4 Types":

从c++ 03“17.4.3.1.4类型”:

For each type T from the Standard C library (footnote 169), the types ::T and std::T are reserved to the implementation and, when defined, ::T shall be identical to std::T.

对于标准C库(footnote 169)中的每一种类型T,类型为:T和std::T是为实现保留的,在定义时,T与std相同::T。

And footnote 169:

169年,脚注:

These types are clock_t, div_t, FILE, fpos_t, lconv, ldiv_t, mbstate_t, ptrdiff_t, sig_atomic_t, size_t, time_t, tm, va_list, wctrans_t, wctype_t, and wint_t.

这些类型包括clock_t、div_t、FILE、fpos_t、lconv、ldiv_t、mbstate_t、ptrdiff_t、sig_atomic_t、size_t、time_t、tm、va_list、wctrans_t、wctype_t和wint_t。

#3


13  

std::size_t is in fact stddef.h's size_t.

size_t实际上是stddef。h size_t。

cstddef gives the following:

cstddef给以下:

#include <stddef.h>
namespace std 
{
  using ::ptrdiff_t;
  using ::size_t;
}

...effectively bringing the previous definition into the std namespace.

…有效地将前面的定义引入std名称空间。

#1


66  

C's size_t and C++'s std::size_t are both same.

C的size_t和c++的std::size_t都是一样的。

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中,它在 和c++中,其定义在 中,其内容与C头相同(参见下面的引用)。它的定义为无符号整数类型,是sizeof运算符的结果。 中定义。h>

C Standard says in §17.7/2,

C标准在第17.7/2节,

size_t which is the unsigned integer type of the result of the sizeof operator

size_t,它是sizeof运算符结果的无符号整数类型。

And C++ Standard says (about cstddef header) in §18.1/3,

C++标准表示(关于cstddef头)

The contents are the same as the Standard C library header , with the following changes.

内容与标准C库标题相同,并有以下更改。

So yeah, both are same; the only difference is that C++ defines size_t in std namespace.

所以,两者都是一样的;唯一的区别是,c++定义了std名称空间中的size_t。

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.

请注意,上面的行还说了“with the following changes”,这并不是指size_t。它更倾向于将c++(大部分)添加到语言中(不存在于C中),而在相同的标题中也定义了这些内容。


Wikipedia has very good info about range and storage size of size_t:

Wikipedia对size_t的范围和存储大小有非常好的信息:

Range and storage size of size_t

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,[3][4] when moving from 32 to 64-bit architecture, for example.

实际类型的size_t是平台相关的;一个常见的错误是假设size_t与unsigned int相同,这可能导致编程错误,[3][4]在从32位架构迁移到64位架构时,例如。

According to the 1999 ISO C standard (C99), size_t is an unsigned integer type of at least 16 bits.

根据1999年ISO C标准(C99), size_t是一个无符号整数类型,至少16位。

And the rest you can read from this page at wikipedia.

剩下的你可以在*上看到。

#2


15  

From C++03 "17.4.3.1.4 Types":

从c++ 03“17.4.3.1.4类型”:

For each type T from the Standard C library (footnote 169), the types ::T and std::T are reserved to the implementation and, when defined, ::T shall be identical to std::T.

对于标准C库(footnote 169)中的每一种类型T,类型为:T和std::T是为实现保留的,在定义时,T与std相同::T。

And footnote 169:

169年,脚注:

These types are clock_t, div_t, FILE, fpos_t, lconv, ldiv_t, mbstate_t, ptrdiff_t, sig_atomic_t, size_t, time_t, tm, va_list, wctrans_t, wctype_t, and wint_t.

这些类型包括clock_t、div_t、FILE、fpos_t、lconv、ldiv_t、mbstate_t、ptrdiff_t、sig_atomic_t、size_t、time_t、tm、va_list、wctrans_t、wctype_t和wint_t。

#3


13  

std::size_t is in fact stddef.h's size_t.

size_t实际上是stddef。h size_t。

cstddef gives the following:

cstddef给以下:

#include <stddef.h>
namespace std 
{
  using ::ptrdiff_t;
  using ::size_t;
}

...effectively bringing the previous definition into the std namespace.

…有效地将前面的定义引入std名称空间。