data model
Datetype | LP64 | ILP64 | LLP64 | ILP32 | LP32 |
---|---|---|---|---|---|
char | 8 | 8 | 8 | 8 | 8 |
short | 16 | 16 | 16 | 16 | 16 |
_int32 | 32 | ||||
int | 32 | 64 | 32 | 32 | 16 |
long | 64 | 64 | 32 | 32 | 32 |
long long | 64 | 64 | 64 | 64 | 64 |
pointer | 64 | 64 | 64 | 32 | 32 |
C 语言定义了一些基本数据类型,但是没有给出数据类型实际的位宽。随着 64bit 处理器的大量使用,操作系统等软件就需要选择匹配上这些数据类型,由此产生出了上表列出的各种数据模型。
业界对 64-bit 寻址提出了三种数据模型,它们分别是 LP64, ILP64, LLP64
。至于 SIPL64
这里不列了,顾名思义,short
也是 64bit 位宽的,使用非常之少。
LP64 (also known as 4/8/8) denotes long and pointer as 64 bit types, ILP64 (also known as 8/8/8) means int, long and pointer are 64 bit types and LLP64 (also known as 4/4/8) adds a new type (long long) and pointer as 64 bit types.
Most of today’s 32 bit systems are ILP32 (that is, int, long and pointers are all 32-bits wide).
The majority of C Language programs written today for Microsoft Windows 3.1 are written for the Win-16 APIs which is an LP32 (int is 16 bits, while long and pointers are 32-bits) model. The C definitions on the Apple Macintosh are also LP32.
实际上当今 64-bit 的操作系统选择的数据模型几乎是 LP64 或者 LLP64。其中 LP64 的阵营是 类 unix 的操作系统,比如 Linux,后者则是微软的 windows。这篇文章介绍了为什么 LP64
是更优的选择1。LLP64
可以看成是 ILP32
带了 64-bit 的指针,据说微软的代码用 long
比较多,而 Unix 则是 int
居多,因此微软选择了 LLP64
,如此其数据结构中不包含指针的就能和 IPL32
保持一致2。
C 标准只规定了基本数据类型的最小长度,至于实际位宽,由选择的数据模型决定,通常是操作系统占据了主导地位,数据模型是 ABI 标准的一部分。当然 64bit 操作系统上是允许多个数据模型存在的,比如同时支持 LP64
和 ILP32
(构建32位的程序时)。
日常代码的编写常常隐含着对数据类型宽度的假设,因此,当将指针转换为整数对象时, C 代码应首选 (u)intptr_t
而不是 long。