简体   繁体   English

为什么size_t更好?

[英]Why is size_t better?

The title is actually a bit misleading, but I wanted to keep it short. 标题实际上有点误导,但我想保持简短。 I've read about why I should use size_t and I often found statements like this: 我已经读过为什么我应该使用size_t,我经常发现这样的语句:

size_t is guaranteed to be able to express the maximum size of any object, including any array size_t保证能够表达任何对象的最大大小,包括任何数组

I don't really understand what that means. 我真的不明白这意味着什么。 Is there some kind of cap on how much memory you can allocate at once and size_t is guaranteed to be large enough to count every byte in that memory block? 是否可以一次分配多少内存,并且size_t保证足够大以计算该内存块中的每个字节?

Follow-up question: 后续问题:
What determines how much memory can be allocated? 是什么决定了可以分配多少内存?

Let's say the biggest object your compiler/platform can have is 4 gb. 假设您的编译器/平台可以拥有的最大对象是4 GB。 size_t then is 32 bit. size_t则为32位。 Now let's say you recompile your program on a 64 bit platform able to support objects of size 2^43 - 1. size_t will be at least 43 bit long (but normally it will be 64 bit at this point). 现在让我们假设您在64位平台上重新编译您的程序,该平台能够支持大小为2 ^ 43的对象 - 1. size_t将至少为43位长(但通常在此时为64位)。 The point is that you only have to recompile the program. 关键是你只需要重新编译程序。 You don't have to change all your int s to long (if int is 32 bit and long is 64 bit) or from int32_t to int64_t . 您不必将所有int更改为long (如果int为32位且long为64位)或从int32_t更改为int64_t (if you are asking yourself why 43 bit, let's say that Windows Server 2008 R2 64bit doesn't support objects of size 2^63 nor objects of size 2^62... It supports 8 TB of addressable space... So 43 bit!) (如果你问自己为什么43位,让我们说Windows Server 2008 R2 64bit不支持大小为2 ^ 63的对象,也不支持大小为2 ^ 62的对象......它支持8 TB的可寻址空间...所以43位!)

Many programs written for Windows considered a pointer to be as much big as a DWORD (a 32 bit unsigned integer). 为Windows编写的许多程序认为指针与DWORD (32位无符号整数)一样大。 These programs can't be recompiled on 64 bit without rewriting large swats of code. 如果不重写大型代码,则无法在64位上重新编译这些程序。 Had they used DWORD_PTR (an unsigned value guaranteed to be as much big as necessary to contain a pointer) they wouldn't have had this problem. 如果他们使用DWORD_PTR (保证无符号值保证包含指针所需的大小),他们就不会遇到这个问题。

The size_t "point" is the similar. size_t “点”是相似的。 but different ! 但不同

size_t isn't guaranteed to be able to contain a pointer!! size_t不能保证能够包含指针!!
(the DWORD_PTR of Microsoft Windows is) (Microsoft Windows的DWORD_PTR是)

This, in general, is illegal: 一般来说,这是非法的:

void *p = ...
size_t p2 = (size_t)p;

For example, on the old DOS "platform", the maximum size of an object was 64k, so size_t needed to be 16 bit BUT a far pointer needed to be at least 20 bit, because the 8086 had a memory space of 1 mb (in the end a far pointer was 16 + 16 bit, because the memory of an 8086 was segmented) 例如,旧的DOS“平台”上,物体的最大尺寸是64K,所以size_t需要的是16位, 至少为20位需要一个远指针,因为8086具有1兆位存储器空间(最后一个远指针是16 + 16位,因为8086的内存是分段的)

Basically it means that size_t , is guaranteed to be large enough to index any array and get the size of any data type. 基本上它意味着size_t保证足够大以索引任何数组并获得任何数据类型的大小。

It is preferred over using just int , because the size of int and other integer types can be smaller than what can be indexed. 它比使用just int ,因为int和其他整数类型的大小可以小于可以索引的大小。 For example int is usually 32-bits long which is not enough to index large arrays on 64-bit machines. 例如, int通常为32位长,这不足以索引64位计算机上的大型数组。 (This is actually a very common problem when porting programs to 64-bit.) (将程序移植到64位时,这实际上是一个非常常见的问题。)

That is exactly the reason. 这正是原因所在。 The maximum size of any object in a given programming language is determined by a combination of the OS, the CPU architecture and the compiler/linker in use. 给定编程语言中任何对象的最大大小由OS,CPU体系结构和正在使用的编译器/链接器的组合决定。

size_t is defined to be big enough to hold the size value of the largest possible object. size_t被定义为足以容纳最大可能对象的大小值。

This usually means that size_t is typedef'ed to be the same as the largest int type available. 这通常意味着size_t的typedef与可用的最大int类型相同。 So on a 32 bit environment it would typically be 4 bytes and in a 64 bit system 8 bytes. 因此,在32位环境中,它通常是4个字节,在64位系统中是8个字节。

size_t is defined for the platform that you are compiling for. size_t是为您要编译的平台定义的。 Hence it can represent the maximum for that platform. 因此,它可以代表该平台的最大值。

size_t是sizeof运算符的返回值(参见7.17 c99),因此它必须描述系统可以表示的最大可能对象。

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

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