C++ 我在哪里可以找到 size_t 的定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1119370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where do I find the definition of size_t?
提问by Eliseo Ocampos
I see variables defined with this type but I don't know where it comes from, nor what is its purpose. Why not use int or unsigned int? (What about other "similar" types? Void_t, etc).
我看到用这种类型定义的变量,但我不知道它来自哪里,也不知道它的目的是什么。为什么不使用 int 或 unsigned int?(其他“类似”类型呢?Void_t 等)。
采纳答案by Martin Liversage
From Wikipedia
来自维基百科
The
stdlib.h
andstddef.h
header files define a datatype calledsize_t
1which 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,2particularly as 64-bit architectures become more prevalent.
的
stdlib.h
和stddef.h
头文件定义了一个名为数据类型size_t
1,其用于表示一个对象的大小。采用 size 的库函数期望它们是 typesize_t
,并且 sizeof 运算符的计算结果为size_t
。的实际类型
size_t
是平台相关的;一个常见的错误是假设size_t
它与 unsigned int 相同,这可能会导致编程错误2,尤其是在 64 位体系结构变得更加普遍时。
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
标准头文件中定义了以下类型和宏
stddef.h
<剪辑>
size_t
这是 sizeof 运算符结果的无符号整数类型
回答by stefanB
According to size_t description on en.cppreference.comsize_t
is defined in the following headers :
根据en.cppreference.com上的size_t 描述在size_t
以下标题中定义:
std::size_t
...
Defined in header <cstddef>
Defined in header <cstdio>
Defined in header <cstring>
Defined in header <ctime>
Defined in header <cwchar>
回答by fpmurphy
size_t
is the unsigned integer type of the result of the sizeof operator (ISO C99 Section 7.17.)
size_t
是 sizeof 运算符结果的无符号整数类型(ISO C99 第 7.17 节。)
The sizeof
operator yields the size (in bytes) of its operand, which may be an
expression or the parenthesized name of a type. The size is determined from the type of
the operand. The result is an integer. The value of the result is implementation-de?ned, and
its type (an unsigned integer type) is size_t
(ISO C99 Section 6.5.3.4.)
的sizeof
操作者产生其操作数的大小(以字节为单位),其可以是表达或类型的括号名称。大小由操作数的类型决定。结果是一个整数。结果的值是实现定义的,其类型(无符号整数类型)是size_t
(ISO C99 第 6.5.3.4 节。)
回答by Matt Price
Practically speaking size_t
represents the number of bytes you can address. On most modern architectures for the last 10-15 years that has been 32 bits which has also been the size of a unsigned int. However we are moving to 64bit addressing while the uint
will most likely stay at 32bits (it's size is not guaranteed in the c++ standard). To make your code that depends on the memory size portable across architectures you should use a size_t
. For example things like array sizes should always use size_t
's. If you look at the standard containers the ::size()
always returns a size_t
.
实际上,size_t
代表您可以寻址的字节数。在过去 10-15 年的大多数现代体系结构中,32 位也是 unsigned int 的大小。然而,我们正在转向 64 位寻址,而uint
最有可能保持在 32 位(在 c++ 标准中不保证它的大小)。要使依赖于内存大小的代码跨体系结构可移植,您应该使用size_t
. 例如,数组大小之类的东西应该始终使用size_t
's。如果您查看标准容器,它::size()
总是返回一个size_t
.
Also note, visual studio has a compile option that can check for these types of errors called "Detect 64-bit Portability Issues".
另请注意,visual studio 有一个编译选项,可以检查这些类型的错误,称为“检测 64 位可移植性问题”。
回答by lavinio
This way you always know what the size is, because a specific type is dedicated to sizes. The very own question shows that it can be an issue: is it an int
or an unsigned int
? Also, what is the magnitude (short
, int
, long
, etc.)?
通过这种方式,您始终知道尺寸是多少,因为特定类型专用于尺寸。自己的问题表明它可能是一个问题:它是一个int
还是一个unsigned int
?此外,什么是幅度(short
,int
,long
等)?
Because there is a specific type assigned, you don't have to worry about the length or the signed-ness.
因为分配了特定类型,所以您不必担心长度或符号。
The actual definition can be found in the C++ Reference Library, which says:
实际定义可以在C++ 参考库中找到,它说:
Type:
size_t
(Unsigned integral type)Header:
<cstring>
size_t
corresponds to the integral data type returned by the language operatorsizeof
and is defined in the<cstring>
header file (among others) as an unsigned integral type.In
<cstring>
, it is used as the type of the parameternum
in the functionsmemchr
,memcmp
,memcpy
,memmove
,memset
,strncat
,strncmp
,strncpy
andstrxfrm
, which in all cases it is used to specify the maximum number of bytes or characters the function has to affect.It is also used as the return type for
strcspn
,strlen
,strspn
andstrxfrm
to return sizes and lengths.
类型:(
size_t
无符号整数类型)标题:
<cstring>
size_t
对应于语言运算符返回的整数数据类型,sizeof
并在<cstring>
头文件(以及其他文件)中定义为无符号整数类型。在
<cstring>
,它被用作参数的类型num
中的函数memchr
,memcmp
,memcpy
,memmove
,memset
,strncat
,strncmp
,strncpy
和strxfrm
,其中在所有情况下它是用来指定字节或字符的功能有可能影响的最大数目。它也可以用来作为返回类型为
strcspn
,strlen
,strspn
和strxfrm
对返回的尺寸和长度。
回答by Ryan
size_t should be defined in your standard library's headers. In my experience, it usually is simply a typedef to unsigned int. The point, though, is that it doesn't have to be. Types like size_t allow the standard library vendor the freedom to change its underlying data types if appropriate for the platform. If you assume size_t is always unsigned int (via casting, etc), you could run into problems in the future if your vendor changes size_t to be e.g. a 64-bit type. It is dangerous to assume anything about this or any other library type for this reason.
size_t 应该在标准库的头文件中定义。根据我的经验,它通常只是 unsigned int 的 typedef。不过,关键是它不一定是。像 size_t 这样的类型允许标准库供应商在适合平台的情况下自由更改其底层数据类型。如果您假设 size_t 始终是 unsigned int(通过强制转换等),那么如果您的供应商将 size_t 更改为例如 64 位类型,您将来可能会遇到问题。出于这个原因,假设有关此或任何其他库类型的任何信息都是危险的。
回答by Michael Burr
I'm not familiar with void_t
except as a result of a Google search (it's used in a vmalloc
library by Kiem-Phong Vo at AT&T Research- I'm sure it's used in other libraries as well).
void_t
除了 Google 搜索的结果外,我不熟悉(它在 AT&T Research 的 Kiem-Phong Vo的vmalloc
图书馆中使用- 我确定它也用于其他图书馆)。
The various xxx_t typedefs are used to abstract a type from a particular definite implementation, since the concrete types used for certain things might differ from one platform to another. For example:
各种 xxx_t typedef 用于从特定的明确实现中抽象出类型,因为用于某些事物的具体类型可能因平台而异。例如:
- size_t abstracts the type used to hold the size of objects because on some systems this will be a 32-bit value, on others it might be 16-bit or 64-bit.
Void_t
abstracts the type of pointer returned by thevmalloc
library routines because it was written to work on systems that pre-date ANSI/ISO C where thevoid
keyword might not exist. At least that's what I'd guess.wchar_t
abstracts the type used for wide characters since on some systems it will be a 16 bit type, on others it will be a 32 bit type.
- size_t 抽象了用于保存对象大小的类型,因为在某些系统上这将是一个 32 位值,在其他系统上它可能是 16 位或 64 位。
Void_t
抽象了vmalloc
库例程返回的指针类型,因为它是为在早于 ANSI/ISO C 的系统上工作而编写的,其中该void
关键字可能不存在。至少我是这么猜的。wchar_t
抽象用于宽字符的类型,因为在某些系统上它将是 16 位类型,在其他系统上它将是 32 位类型。
So if you write your wide character handling code to use the wchar_t
type instead of, say unsigned short
, that code will presumably be more portable to various platforms.
因此,如果您编写宽字符处理代码以使用wchar_t
类型而不是,例如unsigned short
,该代码可能更易于移植到各种平台。
回答by alfC
In minimalistic programs where a size_t
definition was not loaded "by chance" in some include but I still need it in some context (for example to access std::vector<double>
), then I use thatcontext to extract the correct type. For example typedef std::vector<double>::size_type size_t
.
在size_t
一些包含的定义不是“偶然”加载的简约程序中,但我在某些上下文中仍然需要它(例如访问std::vector<double>
),然后我使用该上下文来提取正确的类型。例如typedef std::vector<double>::size_type size_t
。
(Surround with namespace {...}
if necessary to make the scope limited.)
(namespace {...}
如有必要,请环绕以限制范围。)
回答by Crowman
As for "Why not use int or unsigned int?", simply because it's semantically more meaningful not to. There's the practical reason that it can be, say, typedef
d as an int
and then upgraded to a long
later, without anyone having to change their code, of course, but more fundamentally than that a type is supposed to be meaningful. To vastly simplify, a variable of type size_t
is suitable for, and used for, containing the sizes of things, just like time_t
is suitable for containing time values. How these are actually implemented should quite properly be the implementation's job. Compared to just calling everything int
, using meaningful typenames like this helps clarify the meaning and intent of your program, just like any rich set of types does.
至于“为什么不使用 int 或 unsigned int?”,仅仅是因为它在语义上更有意义。有一个实际的原因是它可以是,比如说,typedef
d as anint
然后升级到 a long
later,当然,不需要任何人更改他们的代码,但更根本的是一个类型应该是有意义的。为了大大简化,类型变量size_t
适用于并用于包含事物的大小,就像time_t
适用于包含时间值一样。如何实际实现这些应该是实现的工作。与仅仅调用 everything 相比int
,使用像这样有意义的类型名有助于阐明程序的含义和意图,就像任何丰富的类型集一样。