32位上的sizeof(size_t)与各种64位数据模型有什么区别?

时间:2021-04-12 16:23:09

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?

在64位系统上,sizeof(unsigned long)取决于系统实现的数据模型,例如,它在LLP64 (Windows)上是4字节,在LP64 (Linux,等等)上是8字节。sizeof(size_t)应该是什么?它是否与数据模型(比如sizeof(long))不同?如果是这样,如何?


References:

引用:

64-bit data models on Wikipedia

*上的64位数据模型

4 个解决方案

#1


47  

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).

size_t由C标准定义为sizeof运算符的无符号整数返回类型(c996.3.5.4.4),以及malloc和friends的参数(c997.20.3.3等)。实际的范围设置为最大(SIZE_MAX)至少为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.

然而,这并不能让我们确定sizeof(size_t)。实现可以*地使用它喜欢的size_t的任何表示,因此没有上限,而且实现也可以*地定义一个16位的字节,在这种情况下,size_t可以等价于无符号字符。

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:

尽管如此,总的来说,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.

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.

您将注意到指针在所有情况下都是64位的;而且拥有64位指针也没有什么意义,但毕竟64位指针的大小并不重要。

#2


7  

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.

它应该随体系结构而变化,因为它表示任何对象的大小。所以在32位系统中,size_t可能至少有32位宽。在64位系统上,它可能至少是64位宽。

#3


4  

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

编辑:谢谢您的评论——我在C99标准中查找过,它在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)

结果的值是实现定义的,其类型(无符号整数类型)是size_t,在 (和其他标题) 中定义。h>

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:

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

limit of size_t

size_t极限

SIZE_MAX 65535

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.

这基本上意味着,不管size_t的大小,允许的值范围是0-65535,其余都是依赖于实现的。

#4


1  

size_t is 64 bit normally on 64 bit machine

size_t在64位机器上是正常的64位

#1


47  

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).

size_t由C标准定义为sizeof运算符的无符号整数返回类型(c996.3.5.4.4),以及malloc和friends的参数(c997.20.3.3等)。实际的范围设置为最大(SIZE_MAX)至少为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.

然而,这并不能让我们确定sizeof(size_t)。实现可以*地使用它喜欢的size_t的任何表示,因此没有上限,而且实现也可以*地定义一个16位的字节,在这种情况下,size_t可以等价于无符号字符。

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:

尽管如此,总的来说,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.

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.

您将注意到指针在所有情况下都是64位的;而且拥有64位指针也没有什么意义,但毕竟64位指针的大小并不重要。

#2


7  

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.

它应该随体系结构而变化,因为它表示任何对象的大小。所以在32位系统中,size_t可能至少有32位宽。在64位系统上,它可能至少是64位宽。

#3


4  

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

编辑:谢谢您的评论——我在C99标准中查找过,它在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)

结果的值是实现定义的,其类型(无符号整数类型)是size_t,在 (和其他标题) 中定义。h>

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:

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

limit of size_t

size_t极限

SIZE_MAX 65535

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.

这基本上意味着,不管size_t的大小,允许的值范围是0-65535,其余都是依赖于实现的。

#4


1  

size_t is 64 bit normally on 64 bit machine

size_t在64位机器上是正常的64位