32 位与各种 64 位数据模型的 sizeof(size_t) 是多少? [英] What's sizeof(size_t) on 32-bit vs the various 64-bit data models?

查看:29
本文介绍了32 位与各种 64 位数据模型的 sizeof(size_t) 是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<小时>

参考文献:

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

解决方案

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 位.一般数据模型只影响静态数据;例如,在海湾合作委员会中:

`-mcmodel=small'为小代码模型生成代码:程序及其符号必须链接在地址空间的低 2 GB 中.指针是 64 位的.程序可以是静态的或动态的链接.这是默认的代码模型.`-mcmodel=内核'为内核代码模型生成代码.内核运行在地址空间的负 2 GB.该模型必须用于Linux内核代码.`-mcmodel=中'为中型模型生成代码:程序链接在地址空间的低 2 GB 但可以定位符号地址空间中的任何位置.程序可以是静态的或动态链接,但不构建共享库支持中等型号.`-mcmodel=大'为大型模型生成代码:此模型不做任何假设关于段的地址和大小.

您会注意到指针在所有情况下都是 64 位的;毕竟,拥有 64 位指针而不是 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

解决方案

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.

这篇关于32 位与各种 64 位数据模型的 sizeof(size_t) 是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆