size_t的正确定义是什么? [英] What is the correct definition of size_t?
问题描述
首先,正确的定义"是什么意思?
First of all, what do I mean, by 'correct definition`?
例如,"C编程语言"第二版.中的K& R,在 2.2数据类型和大小部分中,非常清楚地说明了整数:>
For example, K&R in "C Programming Language" 2nd ed., in section 2.2 Data Types and Sizes, make very clear statements about integers:
- 整数类型有
short
,int
和long
.需要它们来代表不同边界的值.int
是用于特定硬件的自然"大小的数字,因此可能也是最快的数字.- 整数类型
short
,int
和long
的大小完全取决于实现.- 但是它们有限制.
short
和int
至少应保留16位.long
至少应包含32位.short
> =int
> =long
.
- There are
short
,int
andlong
for integer types. They are needed to repesent values of different boundaries.int
is a "naturally" sized number for a specific hardware, so also probably the most fastest.- Sizes for integer types
short
,int
andlong
are purely implementation-dependent.- But they have restrictions.
short
andint
shall hold at least 16 bits.long
shall hold at least 32 bits.short
>=int
>=long
.
这是非常明确和明确的.而size_t
类型则不是这种情况.在K& R 5.4地址算法中,他们说:
That's very clear and unambiguous. And that is not the case for size_t
type. In K&R 5.4 Address arithmetic, they say:
- ...
size_t
是sizeof
运算符返回的无符号整数类型.sizeof
运算符产生存储其操作数类型的对象所需的字节数.
- ...
size_t
is the unsigned integer type returned by thesizeof
operator.- The
sizeof
operator yields the number of bytes required to store an object of the type of its operand.
在 C99标准草案 ,在 6.5.3.4 sizeof运算符中,他们说:
- 结果的值是实现定义的,其类型(无符号整数类型)是
size_t
,在<stddef.h>
中定义(以及其他 标头).
- The value of the result is implementation-defined, and its type (an unsigned integer type) is
size_t
, defined in<stddef.h>
(and other headers).
在 7.17常用定义中:
size_t
是sizeof运算符结果的无符号整数类型;
size_t
which is the unsigned integer type of the result of the sizeof operator;
在 7.18.3其他整数类型的限制中:
- size_t的限制
SIZE_MAX
65535
- limit of size_t
SIZE_MAX
65535
There is also a useful article - Why size_t matters. It says the following:
- 好吧,让我们尝试想象一下,如果没有
size_t
将会怎样.- 例如,让我们从
中获取<string.h>
void *memcpy(void *s1, void const *s2, size_t n);
标准功能- 让
n
参数使用int
代替size_t
.- 但是内存大小不能为负数,所以我们最好采用
unsigned int
.- 很好,好像我们现在很高兴,没有
size_t
.- 但是
unsigned int
的大小有限-如果有一台机器可以复制大于unsigned int
可以容纳的内存块,那该怎么办?- 好的,让我们使用
unsigned long
,现在,我们很高兴吗?- 但是对于那些使用较小内存块运行的机器,
unsigned long
效率低下,因为long
不是自然的" 对他们来说,他们必须执行其他操作才能与long
s.- 所以我们来说明为什么我们需要
size_t
-表示特定硬件可以立即运行的内存大小.在某些机器上 等于int
,其他则等于long
,具体取决于哪种类型 它们是最有效的.
- Okay, let's try to imagine, what it would be if there would be no
size_t
.- For example, let's take
void *memcpy(void *s1, void const *s2, size_t n);
standard function from<string.h>
- Let's use
int
instead ofsize_t
forn
parameter.- But size of memory can't be negative, so let's better take
unsigned int
.- Good, seems like we are happy now and without
size_t
.- But
unsigned int
has limited size - what if there is some machine, that can copy chunks of memory larger thanunsigned int
can hold?- Okay, let's use
unsigned long
then, now we are happy?- But for those machines, which operate with smaller memory chunks,
unsigned long
would be inefficient, becauselong
is not "natural" for them, they must perform additional operations to work withlong
s.- So let's why we need
size_t
- to represent a size of memory, that particular hardware can operate at once. On some machines it would be equal toint
, on others - tolong
, depending on with which type they are most efficient.
据我了解,size_t
受sizeof
运算符严格限制.因此,size_t
表示对象的最大大小(以字节为单位).它还可能代表特定CPU模型可以立即移动的字节数.
What I understand from it is that size_t
is strictly bounded with sizeof
operator. And therefore size_t
represents a maximum size of an object in bytes. It might also represent a number of bytes that particular CPU model can move at once.
但是在这里我仍然有很多谜团:
But there is still much of mystery for me here:
- 就C而言,对象"是什么?
- 为什么它限制为65535(这是最大数目,可以用16位表示)? embedded.com 上的文章说,
size_t
也可能是32位. - K& R说,
int
具有平台的自然"大小,并且可以等于int
或long
.那么,如果它是自然的",为什么不使用它代替size_t
?
- What is "object" in terms of C?
- Why it's limited to 65535, which is maximum number, that could be represented by 16 bits? The article on embedded.com says, that
size_t
could be 32 bit too. - K&R says, that
int
has "natural" size for the platform, and it can be equal toint
or tolong
. So why not use it instead ofsize_t
if it's "natural"?
有类似的问题:
但是它的答案并没有提供明确的定义或权威来源的链接(如果不算Wikipedia这样的话).
But the answers for it doesn't provide clear definition or links to authoritative sources (if not count Wikipedia as such).
我想知道什么时候使用size_t
,什么时候不使用size_t
,为什么引入它以及它真正代表什么.
I want to know when to use size_t
, when not to use size_t
, why it was introduced, and what it really represents.
推荐答案
就C而言,对象"是什么?
What is "object" in terms of C?
对象"是定义的术语. C99标准将其定义为:执行环境中的数据存储区域,其内容可以表示值"(第3.14节).通俗的定义可能是在内存中存储一个值".对象的大小不同,具体取决于存储的值的类型.该类型不仅包括诸如char
和int
之类的简单类型,还包括诸如结构和数组之类的复杂类型.例如,数组的存储是 object ,其中每个元素的 object .
"Object" is a defined term. The C99 standard defines it as: "region of data storage in the execution environment, the contents of which can represent values" (section 3.14). A more colloquial definition might be "the storage in memory for a value." Objects come in different sizes, depending on the type of the value stored. That type includes not only simple types such as char
and int
, but also complex types such as structures and arrays. For example, the storage for an array is an object, within which is an object for each element.
为什么它限制为65535(这是最大数目,可以用16位表示)? Embedded.com上的文章说,size_t也可能是32位.
Why it's limited to 65535, which is maximum number, that could be represented by 16 bits? The article on embedded.com says, that size_t could be 32 bit too.
您误会了.重新阅读第7.18.3节的前两段. SIZE_MAX
表示类型size_t
的最大值,但其实际值取决于实现.标准中给出的值为可以的 minimum 值.在大多数实现中,它更大.
You misunderstand. Re-read the first two paragraphs of section 7.18.3. SIZE_MAX
represents the maximum value of type size_t
, but its actual value is implementation dependent. The value given in the standard is the minimum value that that can be. In most implementations it is larger.
K& R说,int对于平台具有自然"的大小,并且可以等于int或long.那么,如果它是自然"的,为什么不使用它而不是size_t?
K&R says, that int has "natural" size for the platform, and it can be equal to int or to long. So why not use it instead of size_t if it's "natural"?
因为没有特别的原因将对象的最大大小限制为单个机器字可表示的字节数(这几乎是自然大小"的含义).同样,在int
和long
的大小不同的地方,也不清楚哪个应该对应size_t
.使用size_t
而不是其中之一来提取机器详细信息,并使您的代码更具可移植性.
Because there is no particular reason that the maximum size of an object should be limited to the number of bytes expressible in a single machine word (which is pretty much what the "natural size" means). Nor, where int
and long
differ in size, is it clear which one should correspond to size_t
, if either. Using size_t
instead of one of these abstracts away machine details and makes your code more portable.
响应更新:
我想知道什么时候使用size_t,什么时候不使用size_t,为什么引入它以及它真正代表什么.
I want to know, when to use size_t, when to not use size_t, why it was introduced and what it really represents.
size_t
主要定义为sizeof
的结果类型.因此,它真正代表"的是对象的大小.
size_t
is primarily defined as the type of the result of sizeof
. It follows that what it "really represents" is the size of an object.
使用size_t
保存代表对象大小或与对象大小有关的值.这就是它的目的.在大多数情况下,您可以通过类型匹配来实现:使用类型为size_t
的变量存储声明为具有该类型的值,例如某些函数(例如strlen()
)的返回值和某些运算符的结果(例如sizeof
).
Use size_t
to hold values that represent or are related to the size of an object. That is expressly what it's for. For the most part, you can accomplish this by type matching: use variables of type size_t
to store the values declared to have that type, such as the return values of certain functions (e.g. strlen()
) and the results of certain operators (e.g. sizeof
).
对于表示除对象大小以外的其他内容或紧密相关的内容(例如对象大小的总和或正差)的值,请勿使用size_t
.
Do not use size_t
for values that represent something other than an object size or something closely related (such as the sum or positive difference of object sizes).
这篇关于size_t的正确定义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!