为什么 size_t 更好?

发布于 2024-12-11 08:29:13 字数 393 浏览 0 评论 0原文

这个标题实际上有点误导,但我想保持简短。我读过为什么应该使用 size_t 并且经常发现这样的语句:

size_t 保证能够表达任何对象(包括任何数组)的最大大小

我不太明白这意味着什么。一次可以分配多少内存是否有某种上限,并且 size_t 保证足够大以计算该内存块中的每个字节?

后续问题:
什么决定了可以分配多少内存?

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 is guaranteed to be able to express the maximum size of any object, including any array

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?

Follow-up question:
What determines how much memory can be allocated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

泛滥成性 2024-12-18 08:29:13

假设您的编译器/平台可以拥有的最大对象是 4 GB。 size_t 则为 32 位。现在假设您在能够支持大小为 2^43 - 1 的对象的 64 位平台上重新编译程序。 size_t 的长度将至少为 43 位(但通常此时为 64 位) )。重点是你只需要重新编译程序即可。您不必将所有 int 更改为 long(如果 int 是 32 位且 long 是64 位)或从 int32_tint64_t
(如果您问自己为什么是 43 位,那么假设 Windows Server 2008 R2 64 位不支持大小为 2^63 的对象,也不支持大小为 2^62 的对象...它支持 8 TB可寻址空间...所以 43 位!)

许多为 Windows 编写的程序认为指针与 DWORD(32 位无符号整数)一样大。如果不重写大量代码,这些程序就无法在 64 位上重新编译。如果他们使用 DWORD_PTR(一个无符号值,保证与包含指针所需的一样大),他们就不会遇到这个问题。

size_t“点”是类似的。 但不同

size_t 不保证能够包含指针!!
(Microsoft Windows 的 DWORD_PTR 是)

这通常是非法的:

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

例如,在旧的 DOS“平台”上,对象的最大大小为 64k,因此 size_t需要是16位但是远指针至少需要20位,因为8086的内存空间为1 mb(最终远指针是16 + 16位,因为8086的内存是分段的)

Let's say the biggest object your compiler/platform can have is 4 gb. size_t then is 32 bit. 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). The point is that you only have to recompile the program. You don't have to change all your ints to long (if int is 32 bit and long is 64 bit) or from int32_t to 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!)

Many programs written for Windows considered a pointer to be as much big as a DWORD (a 32 bit unsigned integer). These programs can't be recompiled on 64 bit without rewriting large swats of code. 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.

The size_t "point" is the similar. but different!

size_t isn't guaranteed to be able to contain a pointer!!
(the DWORD_PTR of Microsoft Windows is)

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)

何其悲哀 2024-12-18 08:29:13

基本上,这意味着 size_t 保证足够大,可以索引任何数组并获取任何数据类型的大小。

它优于仅使用 int,因为 int 和其他整数类型的大小可能小于可索引的大小。例如,int 通常是 32 位长,这不足以在 64 位机器上索引大型数组。 (这实际上是将程序移植到 64 位时很常见的问题。)

Basically it means that size_t, is guaranteed to be large enough to index any array and get the size of any data type.

It is preferred over using just int, because the size of int and other integer types can be smaller than what can be indexed. For example int is usually 32-bits long which is not enough to index large arrays on 64-bit machines. (This is actually a very common problem when porting programs to 64-bit.)

青巷忧颜 2024-12-18 08:29:13

正是这个原因。
给定编程语言中任何对象的最大大小由操作系统、CPU 架构和所使用的编译器/链接器的组合确定。

size_t 被定义为足够大以容纳最大可能对象的大小值。

这通常意味着 size_t 的类型定义为与可用的最大 int 类型相同。
因此,在 32 位环境中通常为 4 个字节,在 64 位系统中为 8 个字节。

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.

size_t is defined to be big enough to hold the size value of the largest possible object.

This usually means that size_t is typedef'ed to be the same as the largest int type available.
So on a 32 bit environment it would typically be 4 bytes and in a 64 bit system 8 bytes.

风筝在阴天搁浅。 2024-12-18 08:29:13

size_t 是为您正在编译的平台定义的。因此它可以代表该平台的最大值。

size_t is defined for the platform that you are compiling for. Hence it can represent the maximum for that platform.

爱人如己 2024-12-18 08:29:13

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

size_t is the return of the sizeof operator (see 7.17 c99) therefore it must describe the largest possible object the system can represent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文