32 位与各种 64 位数据模型上的 sizeof(size_t) 是多少?

发布于 2024-07-22 07:50:58 字数 365 浏览 10 评论 0原文

在64位系统上,sizeof(unsigned long)取决于系统实现的数据模型,例如LLP64(Windows)上为4字节,LP64(Linux等)上为8字节.)。 sizeof(size_t) 应该是什么? 它是否会像 sizeof(long) 那样随数据模型而变化? 如果是这样,怎么办?


参考文献:

维基百科上的 64 位数据模型

On a 64-bit system, sizeof(unsigned long) depends on the data model implemented by the system, for example, it is 4 bytes on LLP64 (Windows), 8 bytes on LP64 (Linux, etc.). What's sizeof(size_t) supposed to be? Does it vary with data model like sizeof(long) does? If so, how?


References:

64-bit data models on Wikipedia

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

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

发布评论

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

评论(4

凉城凉梦凉人心 2024-07-29 07:50:58

size_t 由 C 标准定义为 sizeof 运算符的无符号整数返回类型(C99 6.3.5.4.4),以及 malloc 和朋友的参数(C99 7.20.3.3 等)。 实际范围设置为最大值 (SIZE_MAX) 至少为 65535 (C99 7.18.3.2)。

然而,这并不能让我们确定 sizeof(size_t)。 该实现可以自由地使用它喜欢的任何 size_t 表示形式 - 因此大小没有上限 - 并且该实现也可以自由地将字节定义为 16 位,在这种情况下 size_t 可以相当于 unsigned char。

不过,抛开这一点不说,一般来说,无论数据模型如何,您在 32 位程序上都会有 32 位 size_t,在 64 位程序上会有 64 位 size_t。 一般来说,数据模型只影响静态数据; 例如,在 GCC 中:

`-mcmodel=small'
     Generate code for the small code model: the program and its
     symbols must be linked in the lower 2 GB of the address space.
     Pointers are 64 bits.  Programs can be statically or dynamically
     linked.  This is the default code model.

`-mcmodel=kernel'
     Generate code for the kernel code model.  The kernel runs in the
     negative 2 GB of the address space.  This model has to be used for
     Linux kernel code.

`-mcmodel=medium'
     Generate code for the medium model: The program is linked in the
     lower 2 GB of the address space but symbols can be located
     anywhere in the address space.  Programs can be statically or
     dynamically linked, but building of shared libraries are not
     supported with the medium model.

`-mcmodel=large'
     Generate code for the large model: This model makes no assumptions
     about addresses and sizes of sections.

您会注意到指针在所有情况下都是 64 位的; 毕竟,拥有 64 位指针但没有 64 位大小没有什么意义。

size_t is defined by the C standard to be the unsigned integer return type of the sizeof operator (C99 6.3.5.4.4), and the argument of malloc and friends (C99 7.20.3.3 etc). The actual range is set such that the maximum (SIZE_MAX) is at least 65535 (C99 7.18.3.2).

However, this doesn't let us determine sizeof(size_t). The implementation is free to use any representation it likes for size_t - so there is no upper bound on size - and the implementation is also free to define a byte as 16-bits, in which case size_t can be equivalent to unsigned char.

Putting that aside, however, in general you'll have 32-bit size_t on 32-bit programs, and 64-bit on 64-bit programs, regardless of the data model. Generally the data model only affects static data; for example, in GCC:

`-mcmodel=small'
     Generate code for the small code model: the program and its
     symbols must be linked in the lower 2 GB of the address space.
     Pointers are 64 bits.  Programs can be statically or dynamically
     linked.  This is the default code model.

`-mcmodel=kernel'
     Generate code for the kernel code model.  The kernel runs in the
     negative 2 GB of the address space.  This model has to be used for
     Linux kernel code.

`-mcmodel=medium'
     Generate code for the medium model: The program is linked in the
     lower 2 GB of the address space but symbols can be located
     anywhere in the address space.  Programs can be statically or
     dynamically linked, but building of shared libraries are not
     supported with the medium model.

`-mcmodel=large'
     Generate code for the large model: This model makes no assumptions
     about addresses and sizes of sections.

You'll note that pointers are 64-bit in all cases; and there's little point to having 64-bit pointers but not 64-bit sizes, after all.

淡淡の花香 2024-07-29 07:50:58

它应该随架构而变化,因为它代表任何对象的大小。 因此,在 32 位系统上,size_t 可能至少为 32 位宽。 在 64 位系统上,它可能至少为 64 位宽。

it should vary with the architecture because it represents the size of any object. So on a 32-bit system size_t will likely be at least 32-bits wide. On a 64-bit system it will likely be at least 64-bit wide.

对你而言 2024-07-29 07:50:58

size_t 在 64 位机器上通常是 64 位

size_t is 64 bit normally on 64 bit machine

眼泪也成诗 2024-07-29 07:50:58

编辑:感谢您的评论 - 我在 C99 标准,第 6.5.3.4 节中说:

结果值为
实现定义的及其类型
无符号整数类型)是 size_t
中定义(以及其他
标题)

因此,没有指定 size_t 的大小,只是它必须是无符号整数类型。 然而,在该标准的第 7.18.3 章中可以找到一个有趣的规范:

size_t的限制

SIZE_MAX 65535

这基本上意味着,无论size_t的大小,允许的值范围是从0-65535,其余的取决于实现。

EDIT: Thanks for the comments - I looked it up in the C99 standard, which says in section 6.5.3.4:

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)

So, the size of size_t is not specified, only that it has to be an unsigned integer type. However, an interesting specification can be found in chapter 7.18.3 of the standard:

limit of size_t

SIZE_MAX 65535

Which basically means that, irrespective of the size of size_t, the allowed value range is from 0-65535, the rest is implementation dependent.

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