简体   繁体   English

size_t是字大小吗?

[英]Is size_t the word size?

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 . 使用g ++解析,我的编译器将size_t视为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? 编译器在内部选择size_t的大小,或者在调用编译器之前, stddef.h中的某些预处理器宏内的size_t实际上是否为字大小?

Or am I way off track? 还是我偏离轨道?

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 ++标准中,[support.types](18.2)/ 6:“类型size_t是一个实现定义的无符号整数类型,它足够大,可以包含任何对象的字节大小。”

This may or may not be the same as a "word size", whatever that means. 无论这意味着什么,这可能与“字大小”相同或不同。

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). size_t不一定是运行代码的机器的“字大小”(在交叉编译的情况下)或者编译代码(在正常情况下代码将在相同类型的编译代码的机器)。 It is an unsigned integer type big enough to hold the size (in bytes) of the largest object that the implementation can allocate. 它是一个无符号整数类型,足以容纳实现可以分配的最大对象的大小(以字节为单位)。


Some history of sizeof and size_t sizeofsize_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 . 我不知道size_t何时被准确引入,但它是在1979年到1989年之间.K&R的第一版1978年的C编程语言没有提到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. 第7版Unix程序员手册根本没有提到size_t ,而且可以追溯到1979年.Kernighan和Pike从1984年开始的“UNIX编程环境”一书中没有提到索引中的size_t (也没有提到malloc()free() ,有点令我惊讶),但这只是指示性的,而不是决定性的。 The C89 standard certainly has size_t . C89标准肯定有size_t

The C99 Rationale documents some information about sizeof() and size_t : C99 Rationale记录了有关sizeof()size_t一些信息:

6.5.3.4 The sizeof operator 6.5.3.4 sizeof运算符

It is fundamental to the correct usage of functions such as malloc and fread that sizeof(char) be exactly one. 正确使用诸如mallocfread之类的函数是正确的, sizeof(char)恰好是一个。 In practice, this means that a byte in C terms is the smallest unit of storage, even if this unit is 36 bits wide; 实际上,这意味着C语言中的一个字节是最小的存储单位,即使该单位是36位宽; 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. 与K&R一样,C89将sizeof运算符的结果定义为无符号整数类型的常量。 Common implementations, and common usage, have often assumed that the resulting type is int . 常见的实现和常见用法通常假设结果类型是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 . 依赖于此行为的旧代码从未被移植到将结果定义为int以外的类型的实现。 The C89 Committee did not feel it was proper to change the language to protect incorrect code. 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. sizeof的类型,无论它是什么,都以size_t形式发布(在库头<stddef.h> ),因为程序员能够引用这种类型是有用的。 This requirement implicitly restricts size_t to be a synonym for an existing unsigned integer type. 此要求隐式地将size_t限制为现有无符号整数类型的同义词。 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. 另请注意,尽管size_t是无符号类型,但sizeof不涉及任何算术运算或转换,如果大小太大而无法表示为size_t ,则会导致模数行为,因此会抛弃任何概念,即最大的可声明对象可能也是如此即使在C89中使用unsigned long或在C99中使用uintmax_t This also restricts the maximum number of elements that may be declared in an array, since for any array a of N elements, 这也限制了可以在数组中声明的元素的最大数量,因为对于N元素的任何数组a

N == sizeof(a)/sizeof(a[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 7.17通用定义

<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 . <stddef.h>是一个发明的标题,用于提供与库一起广泛使用的几种类型和宏的定义: ptrdiff_tsize_twchar_tNULL 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. 包括引用其中一个宏的任何头也将定义它,这是通常的库规则的一个例外,每个宏或函数只属于一个头。

Note that this specifically mentions that the <stddef.h> was invented by the C89 committee. 请注意,这特别提到<stddef.h>是由C89委员会发明的。 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. 我没有发现那些说size_t也是由C89委员会发明的词,但如果不是,那就是C中最新发展的编纂。


In a comment to bmargulies answer , vonbrand says that 'it [ size_t ] is certainly an ANSI-C-ism'. 在对bmargulies 回答的评论中, vonbrand说'it [ size_t ]肯定是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. 我很容易相信它是原始ANSI(ISO)C的创新,尽管有点奇怪,理由并没有说明这一点。

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 . 换句话说, size_t必须足够大以容纳可以从sizeof生成的任何表达式的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). 这可能是机器字大小,但它可能会小得多(例如,如果编译器限制了数组或对象的最大大小)或者大得多(如果编译器允许您创建对象如此之大以至于单个机器) word无法存储该对象的大小)。

Hope this helps! 希望这可以帮助!

size_t was, orignally, just a typedef in sys/types.h (traditionally on Unix/Linux). size_t或者只是sys / types.h中的一个typedef(传统上在Unix / Linux上)。 It was assumed to be 'big enough' for, say, the maximum size of a file, or the maximum allocation with malloc. 例如,假设文件的最大大小或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. 然而,随着时间的推移,标准委员会抓住了它,因此它被复制到许多不同的头文件中,每次都有自己的#ifdef保护来保护多个定义。 On the other hand, the emergence of 64-bit systems with very big potential file sizes clouded its role. 另一方面,具有非常大的潜在文件大小的64位系统的出现使其角色变得模糊。 So it's a bit of a palimpset. 这是一个palimpset。

Language standards now call it out as living in stddef.h. 语言标准现在把它称为生活在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. 请参阅其他答案,了解这些标准对于它有多大的说法。

Such definitions are all implementation defined. 这些定义都是实现定义的。 I would use the sizeof(char *), or maybe sizeof(void *), if I needed a best guess size. 如果我需要最佳猜测大小,我会使用sizeof(char *),或者sizeof(void *)。 The best this gives is the apparent word size software uses... what the hardware really has may be different (eg, a 32-bit system may support 64-bit integers by software). 这给出的最好的是软件使用的明显字大小......硬件真正具有的可能是不同的(例如,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,了解整数大小的各种材料。

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. 尽管定义并未直接说明size_t类型, 甚至不需要最小大小 ,但它间接地给出了一些好的提示。 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必须能够包含任何对象的字节大小,换句话说,它必须能够包含最大可能对象的大小。

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. 因此,通过定义的措辞, size_t在32位体系结构上必须至少为32位,在64位系统上至少为64位。 It is of course possible for an implementation to choose a larger size_t , but this is not usually the case. 实现当然可以选择更大的size_t ,但通常情况并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM