在64位系统上,sizeof(unsigned long)取决于系统实现的数据模型,例如,它在LLP64(Windows)上为4个字节,在LP64(Linux等)上为8个字节.什么是sizeof(size_t)应该是什么?它是否随数据模型而变化sizeof(long)?如果是这样,怎么样?
参考文献:
bdo*_*lan 54
size_t由C标准定义为sizeof运算符的无符号整数返回类型(C99 6.3.5.4.4),以及malloc和friends的参数(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'
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.
Run Code Online (Sandbox Code Playgroud)
你会注意到指针在所有情况下都是64位的; 毕竟,拥有64位指针而不是64位大小毫无意义.
编辑:感谢您的评论 - 我在C99 标准中查找了它,该标准在第 6.5.3.4 节中说:
结果的值是实现定义的,其类型(无符号整数类型)是,在(和其他标头)
size_t中定义<stddef.h>
因此,未指定 的大小size_t,只是它必须是无符号整数类型。然而,在该标准的第 7.18.3 章中可以找到一个有趣的规范:
限制为
size_t
SIZE_MAX 65535
这基本上意味着,无论 的大小如何size_t,允许的值范围是 0-65535,其余的取决于实现。
| 归档时间: |
|
| 查看次数: |
117820 次 |
| 最近记录: |