size_t是单词size吗?

时间:2022-04-18 08:43:29

Is size_t the word size of the machine that compiled the code?

size_t是编译代码的机器的字大小吗?

Parsing with g++, my compiler views size_t as an long unsigned int. Does the compiler internally choose the size of size_t, or is size_t actually typdefed inside some pre-processor macro in stddef.h to the word size before the compiler gets invoked?

使用g++进行解析时,我的编译器将size_t视为一个长而无符号的int类型,编译器是在内部选择size_t的大小,还是在stddef的一些预处理器宏中实际键入了size_t。h在编译器被调用之前的单词大小?

Or am I way off track?

还是我偏离轨道了?

6 个解决方案

#1


21  

In the C++ standard, [support.types] (18.2) /6: "The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object."

在c++标准中,[支持。类型](18.2)/6:“size_t类型是一个实现定义的无符号整数类型,其大小足以包含任何对象的字节大小。”

This may or may not be the same as a "word size", whatever that means.

这可能和“单词大小”一样,也可能不一样,不管它是什么意思。

#2


12  

No; size_t is not necessarily whatever you mean by 'the word size' of the machine that will run the code (in the case of cross-compilation) or that compiled the code (in the normal case where the code will run on the same type of machine that compiled the code). It is an unsigned integer type big enough to hold the size (in bytes) of the largest object that the implementation can allocate.

没有;size_t不一定是你所说的运行代码的机器的“字大小”(在交叉编译的情况下)或者编译代码的机器(在通常的情况下,代码将运行在编译代码的机器上)。它是一个无符号整数类型,足以容纳实现可以分配的最大对象的大小(以字节为单位)。


Some history of sizeof and size_t

I don't know when size_t was introduced exactly, but it was between 1979 and 1989. The 1st Edition of K&R The C Programming Language from 1978 has no mention of size_t. The 7th Edition Unix Programmer's Manual has no mention of size_t at all, and that dates from 1979. The book "The UNIX Programming Environment" by Kernighan and Pike from 1984 has no mention of size_t in the index (nor of malloc() or free(), somewhat to my surprise), but that is only indicative, not conclusive. The C89 standard certainly has size_t.

我不知道size_t是什么时候被引入的,但那是在1979年到1989年之间。1978年第一版的K&R C编程语言没有提到size_t。第7版Unix程序员手册中根本没有提到size_t,这可以追溯到1979年。Kernighan和Pike 1984年出版的《UNIX编程环境》一书在索引中没有提到size_t (malloc()或free(),这让我有些惊讶),但这只是一种指示性,并不是结论性的。C89标准当然有size_t。

The C99 Rationale documents some information about sizeof() and size_t:

C99基本原理文件了一些关于sizeof()和size_t的信息:

6.5.3.4 The sizeof operator

It is fundamental to the correct usage of functions such as malloc and fread that sizeof(char) be exactly one. In practice, this means that a byte in C terms is the smallest unit of storage, even if this unit is 36 bits wide; and all objects are composed of an integer number of these smallest units. Also applies if memory is bit addressable. C89, like K&R, defined the result of the sizeof operator to be a constant of an unsigned integer type. Common implementations, and common usage, have often assumed that the resulting type is int. Old code that depends on this behavior has never been portable to implementations that define the result to be a type other than int. The C89 Committee did not feel it was proper to change the language to protect incorrect code.

对于正确地使用malloc和fread等函数来说,sizeof(char)恰好是一个关键。在实践中,这意味着用C术语表示的字节是最小的存储单元,即使这个单元是36位宽的;所有的对象都由这些最小单位的整数组成。如果内存是位可寻址的,也适用。C89和K&R一样,将sizeof运算符定义为无符号整数类型的常数。共同实现,和共同使用,往往假设结果类型是int。旧的代码依赖于这种行为从未被移植到实现结果定义为一个类型int。C89委员会并不觉得适当的改变语言保护不正确的代码。

The type of sizeof, whatever it is, is published (in the library header <stddef.h>) as size_t, since it is useful for the programmer to be able to refer to this type. This requirement implicitly restricts size_t to be a synonym for an existing unsigned integer type. Note also that, although size_t is an unsigned type, sizeof does not involve any arithmetic operations or conversions that would result in modulus behavior if the size is too large to represent as a size_t, thus quashing any notion that the largest declarable object might be too big to span even with an unsigned long in C89 or uintmax_t in C99. This also restricts the maximum number of elements that may be declared in an array, since for any array a of N elements,

sizeof的类型,无论它是什么,都被作为size_t发布(在库头 中),因为程序员能够引用这种类型是很有用的。这一要求隐式地限制了size_t是现有无符号整数类型的同义词。还要注意,虽然size_t是一个无符号类型,大小不涉及任何算术运算或转换会导致模行为如果尺寸太大代表size_t,因此撤销任何认为最大的应申报的对象可能太大跨度甚至在C89或无符号长uintmax_t C99中。这也限制了可以在数组中声明的元素的最大数量,因为对于任何N个元素的数组a,

N == sizeof(a)/sizeof(a[0])

N = =运算符(a)/ sizeof([0])

Thus size_t is also a convenient type for array sizes, and is so used in several library functions. [...]

因此,size_t对于数组大小也是一种方便的类型,因此在几个库函数中都是如此。[…]

7.17 Common definitions

<stddef.h> is a header invented to provide definitions of several types and macros used widely in conjunction with the library: ptrdiff_t, size_t, wchar_t, and NULL. Including any header that references one of these macros will also define it, an exception to the usual library rule that each macro or function belongs to exactly one header.

< stddef。h>是一个标题,用于提供与库一起广泛使用的几种类型和宏的定义:ptrdiff_t、size_t、wchar_t和NULL。包括引用其中一个宏的任何header也将定义它,这是通常的库规则的一个例外,每个宏或函数都属于一个header。

Note that this specifically mentions that the <stddef.h> was invented by the C89 committee. I've not found words that say that size_t was also invented by the C89 committee, but if it was not, it was a codification of a fairly recent development in C.

注意,这特别提到了 是由C89委员会发明的。我没有发现有文字说size_t也是由C89委员会发明的,但如果不是的话,它是对C语言中最近的发展的编纂。 。h>


In a comment to bmargulies answer, vonbrand says that 'it [size_t] is certainly an ANSI-C-ism'. I can very easily believe that it was an innovation with the original ANSI (ISO) C, though it is mildly odd that the rationale doesn't state that.

在对bmargulies的回答发表评论时,vonbrand表示,“它(size_t)肯定是一种无*主义。”我可以很容易地相信,这是一个创新与原始的ANSI (ISO) C,尽管有点奇怪,理由并不说明。

#3


3  

Not necessarily. The C ISO spec (§17.1/2) defines size_t as

不一定。C ISO规范(§17.1 / 2)size_t定义为

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

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

In other words, size_t has to be large enough to hold the size of any expression that could be produced from sizeof. This could be the machine word size, but it could be dramatically smaller (if, for example, the compiler limited the maximum size of arrays or objects) or dramatically larger (if the compiler were to let you create objects so huge that a single machine word could not store the size of that object).

换句话说,size_t必须大到足以容纳任何可以由sizeof生成的表达式的大小。这可能是机器字大小,但它可以很大程度上减少(例如,如果编译器有限公司的最大大小数组或对象)或显著更大(如果编译器允许您创建对象很大一个机器字不能存储对象的大小)。

Hope this helps!

希望这可以帮助!

#4


2  

size_t was, orignally, just a typedef in sys/types.h (traditionally on Unix/Linux). It was assumed to be 'big enough' for, say, the maximum size of a file, or the maximum allocation with malloc. However, over time, standard committees grabbed it, and so it wound up copied into many different header files, protected each time with its own #ifdef protection from multiple definition. On the other hand, the emergence of 64-bit systems with very big potential file sizes clouded its role. So it's a bit of a palimpset.

size_t在sys/type中只是一个类型定义。h(传统上在Unix / Linux)。它被假定为“足够大”,比如文件的最大大小,或与malloc的最大分配。然而,随着时间的推移,标准委员会抓住了它,因此它被复制到许多不同的头文件中,每次都使用自己的#ifdef保护来保护它不受多个定义的影响。另一方面,具有很大潜在文件大小的64位系统的出现给它的角色蒙上了阴影。这有点像palimpset。

Language standards now call it out as living in stddef.h. It has no necessary relationship to the hardware word size, and no compiler magic. See other answers with respect to what those standards say about how big it is.

语言标准现在称它为生活在stddef.h。它与硬件字大小没有必要的关系,也没有编译器的魔力。关于这些标准说明了它有多大,请参阅其他答案。

#5


0  

Such definitions are all implementation defined. I would use the sizeof(char *), or maybe sizeof(void *), if I needed a best guess size. The best this gives is the apparent word size software uses... what the hardware really has may be different (e.g., a 32-bit system may support 64-bit integers by software).

这些定义都是定义好的实现。我将使用sizeof(char *),或者是sizeof(void *),如果我需要一个最佳的猜测大小。这给人的最好的印象是明显的文字大小软件使用……硬件真正具有的可能是不同的(例如,32位系统可能通过软件支持64位整数)。

Also if you are new to the C languages see stdint.h for all sorts of material on integer sizes.

如果您对C语言不熟悉,请参见stdint。h表示各种材料的整数尺寸。

#6


0  

Although the definition does not directly state what type exactly size_t is, and does not even require a minimum size , it indirectly gives some good hints. A size_t must be able to contain the size in bytes of any object, in other words, it must be able to contain the size of the largest possible object.

尽管定义没有直接声明size_t类型,甚至不需要最小的大小,但它间接地给出了一些很好的提示。size_t必须能够包含任何对象的字节大小,换句话说,它必须能够包含最大对象的大小。

The largest possible object is an array (or structure) with a size equal to the entire available address space. It is not possible to reference a larger object in a meaningful manner, and apart from the availability of swap space there is no reason why it should need to be any smaller.

最大的对象是一个数组(或结构),其大小等于整个可用地址空间。以有意义的方式引用较大的对象是不可能的,而且除了交换空间的可用性之外,也没有理由认为它应该更小。

Therefore, by the wording of the definition, size_t must be at least 32 bits on a 32 bit architecture, and at least 64 bits on a 64 bit system. It is of course possible for an implementation to choose a larger size_t, but this is not usually the case.

因此,根据定义,size_t在32位架构中必须至少是32位,在64位系统中至少是64位。当然,实现可以选择更大的size_t,但通常不是这样。

#1


21  

In the C++ standard, [support.types] (18.2) /6: "The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object."

在c++标准中,[支持。类型](18.2)/6:“size_t类型是一个实现定义的无符号整数类型,其大小足以包含任何对象的字节大小。”

This may or may not be the same as a "word size", whatever that means.

这可能和“单词大小”一样,也可能不一样,不管它是什么意思。

#2


12  

No; size_t is not necessarily whatever you mean by 'the word size' of the machine that will run the code (in the case of cross-compilation) or that compiled the code (in the normal case where the code will run on the same type of machine that compiled the code). It is an unsigned integer type big enough to hold the size (in bytes) of the largest object that the implementation can allocate.

没有;size_t不一定是你所说的运行代码的机器的“字大小”(在交叉编译的情况下)或者编译代码的机器(在通常的情况下,代码将运行在编译代码的机器上)。它是一个无符号整数类型,足以容纳实现可以分配的最大对象的大小(以字节为单位)。


Some history of sizeof and size_t

I don't know when size_t was introduced exactly, but it was between 1979 and 1989. The 1st Edition of K&R The C Programming Language from 1978 has no mention of size_t. The 7th Edition Unix Programmer's Manual has no mention of size_t at all, and that dates from 1979. The book "The UNIX Programming Environment" by Kernighan and Pike from 1984 has no mention of size_t in the index (nor of malloc() or free(), somewhat to my surprise), but that is only indicative, not conclusive. The C89 standard certainly has size_t.

我不知道size_t是什么时候被引入的,但那是在1979年到1989年之间。1978年第一版的K&R C编程语言没有提到size_t。第7版Unix程序员手册中根本没有提到size_t,这可以追溯到1979年。Kernighan和Pike 1984年出版的《UNIX编程环境》一书在索引中没有提到size_t (malloc()或free(),这让我有些惊讶),但这只是一种指示性,并不是结论性的。C89标准当然有size_t。

The C99 Rationale documents some information about sizeof() and size_t:

C99基本原理文件了一些关于sizeof()和size_t的信息:

6.5.3.4 The sizeof operator

It is fundamental to the correct usage of functions such as malloc and fread that sizeof(char) be exactly one. In practice, this means that a byte in C terms is the smallest unit of storage, even if this unit is 36 bits wide; and all objects are composed of an integer number of these smallest units. Also applies if memory is bit addressable. C89, like K&R, defined the result of the sizeof operator to be a constant of an unsigned integer type. Common implementations, and common usage, have often assumed that the resulting type is int. Old code that depends on this behavior has never been portable to implementations that define the result to be a type other than int. The C89 Committee did not feel it was proper to change the language to protect incorrect code.

对于正确地使用malloc和fread等函数来说,sizeof(char)恰好是一个关键。在实践中,这意味着用C术语表示的字节是最小的存储单元,即使这个单元是36位宽的;所有的对象都由这些最小单位的整数组成。如果内存是位可寻址的,也适用。C89和K&R一样,将sizeof运算符定义为无符号整数类型的常数。共同实现,和共同使用,往往假设结果类型是int。旧的代码依赖于这种行为从未被移植到实现结果定义为一个类型int。C89委员会并不觉得适当的改变语言保护不正确的代码。

The type of sizeof, whatever it is, is published (in the library header <stddef.h>) as size_t, since it is useful for the programmer to be able to refer to this type. This requirement implicitly restricts size_t to be a synonym for an existing unsigned integer type. Note also that, although size_t is an unsigned type, sizeof does not involve any arithmetic operations or conversions that would result in modulus behavior if the size is too large to represent as a size_t, thus quashing any notion that the largest declarable object might be too big to span even with an unsigned long in C89 or uintmax_t in C99. This also restricts the maximum number of elements that may be declared in an array, since for any array a of N elements,

sizeof的类型,无论它是什么,都被作为size_t发布(在库头 中),因为程序员能够引用这种类型是很有用的。这一要求隐式地限制了size_t是现有无符号整数类型的同义词。还要注意,虽然size_t是一个无符号类型,大小不涉及任何算术运算或转换会导致模行为如果尺寸太大代表size_t,因此撤销任何认为最大的应申报的对象可能太大跨度甚至在C89或无符号长uintmax_t C99中。这也限制了可以在数组中声明的元素的最大数量,因为对于任何N个元素的数组a,

N == sizeof(a)/sizeof(a[0])

N = =运算符(a)/ sizeof([0])

Thus size_t is also a convenient type for array sizes, and is so used in several library functions. [...]

因此,size_t对于数组大小也是一种方便的类型,因此在几个库函数中都是如此。[…]

7.17 Common definitions

<stddef.h> is a header invented to provide definitions of several types and macros used widely in conjunction with the library: ptrdiff_t, size_t, wchar_t, and NULL. Including any header that references one of these macros will also define it, an exception to the usual library rule that each macro or function belongs to exactly one header.

< stddef。h>是一个标题,用于提供与库一起广泛使用的几种类型和宏的定义:ptrdiff_t、size_t、wchar_t和NULL。包括引用其中一个宏的任何header也将定义它,这是通常的库规则的一个例外,每个宏或函数都属于一个header。

Note that this specifically mentions that the <stddef.h> was invented by the C89 committee. I've not found words that say that size_t was also invented by the C89 committee, but if it was not, it was a codification of a fairly recent development in C.

注意,这特别提到了 是由C89委员会发明的。我没有发现有文字说size_t也是由C89委员会发明的,但如果不是的话,它是对C语言中最近的发展的编纂。 。h>


In a comment to bmargulies answer, vonbrand says that 'it [size_t] is certainly an ANSI-C-ism'. I can very easily believe that it was an innovation with the original ANSI (ISO) C, though it is mildly odd that the rationale doesn't state that.

在对bmargulies的回答发表评论时,vonbrand表示,“它(size_t)肯定是一种无*主义。”我可以很容易地相信,这是一个创新与原始的ANSI (ISO) C,尽管有点奇怪,理由并不说明。

#3


3  

Not necessarily. The C ISO spec (§17.1/2) defines size_t as

不一定。C ISO规范(§17.1 / 2)size_t定义为

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

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

In other words, size_t has to be large enough to hold the size of any expression that could be produced from sizeof. This could be the machine word size, but it could be dramatically smaller (if, for example, the compiler limited the maximum size of arrays or objects) or dramatically larger (if the compiler were to let you create objects so huge that a single machine word could not store the size of that object).

换句话说,size_t必须大到足以容纳任何可以由sizeof生成的表达式的大小。这可能是机器字大小,但它可以很大程度上减少(例如,如果编译器有限公司的最大大小数组或对象)或显著更大(如果编译器允许您创建对象很大一个机器字不能存储对象的大小)。

Hope this helps!

希望这可以帮助!

#4


2  

size_t was, orignally, just a typedef in sys/types.h (traditionally on Unix/Linux). It was assumed to be 'big enough' for, say, the maximum size of a file, or the maximum allocation with malloc. However, over time, standard committees grabbed it, and so it wound up copied into many different header files, protected each time with its own #ifdef protection from multiple definition. On the other hand, the emergence of 64-bit systems with very big potential file sizes clouded its role. So it's a bit of a palimpset.

size_t在sys/type中只是一个类型定义。h(传统上在Unix / Linux)。它被假定为“足够大”,比如文件的最大大小,或与malloc的最大分配。然而,随着时间的推移,标准委员会抓住了它,因此它被复制到许多不同的头文件中,每次都使用自己的#ifdef保护来保护它不受多个定义的影响。另一方面,具有很大潜在文件大小的64位系统的出现给它的角色蒙上了阴影。这有点像palimpset。

Language standards now call it out as living in stddef.h. It has no necessary relationship to the hardware word size, and no compiler magic. See other answers with respect to what those standards say about how big it is.

语言标准现在称它为生活在stddef.h。它与硬件字大小没有必要的关系,也没有编译器的魔力。关于这些标准说明了它有多大,请参阅其他答案。

#5


0  

Such definitions are all implementation defined. I would use the sizeof(char *), or maybe sizeof(void *), if I needed a best guess size. The best this gives is the apparent word size software uses... what the hardware really has may be different (e.g., a 32-bit system may support 64-bit integers by software).

这些定义都是定义好的实现。我将使用sizeof(char *),或者是sizeof(void *),如果我需要一个最佳的猜测大小。这给人的最好的印象是明显的文字大小软件使用……硬件真正具有的可能是不同的(例如,32位系统可能通过软件支持64位整数)。

Also if you are new to the C languages see stdint.h for all sorts of material on integer sizes.

如果您对C语言不熟悉,请参见stdint。h表示各种材料的整数尺寸。

#6


0  

Although the definition does not directly state what type exactly size_t is, and does not even require a minimum size , it indirectly gives some good hints. A size_t must be able to contain the size in bytes of any object, in other words, it must be able to contain the size of the largest possible object.

尽管定义没有直接声明size_t类型,甚至不需要最小的大小,但它间接地给出了一些很好的提示。size_t必须能够包含任何对象的字节大小,换句话说,它必须能够包含最大对象的大小。

The largest possible object is an array (or structure) with a size equal to the entire available address space. It is not possible to reference a larger object in a meaningful manner, and apart from the availability of swap space there is no reason why it should need to be any smaller.

最大的对象是一个数组(或结构),其大小等于整个可用地址空间。以有意义的方式引用较大的对象是不可能的,而且除了交换空间的可用性之外,也没有理由认为它应该更小。

Therefore, by the wording of the definition, size_t must be at least 32 bits on a 32 bit architecture, and at least 64 bits on a 64 bit system. It is of course possible for an implementation to choose a larger size_t, but this is not usually the case.

因此,根据定义,size_t在32位架构中必须至少是32位,在64位系统中至少是64位。当然,实现可以选择更大的size_t,但通常不是这样。