为什么 64 位 Windows 上原生长原语的大小只有 4 个字节?
Why is the size of a native long primitive on 64-bit Windows only 4 bytes?
有人能告诉我这有什么意义,以及如何让它停止吗?说真的,我是疯了还是 64 位 Windows 长类型只有 4 个字节?这有什么意义?我认为本机 long primitive 大小应该与本机寄存器大小相同。
[32-bit Linux]
me@u32:~$ ./sizes32
sizeof(char): 1
sizeof(short): 2
sizeof(int): 4
sizeof(long): 4
sizeof(long long): 8
[64-bit Linux]
me@u64:~$ ./sizes64
sizeof(char): 1
sizeof(short): 2
sizeof(int): 4
sizeof(long): 8
sizeof(long long): 8
[32-bit Windows]
C:\Users\me\Downloads>sizes32.exe
sizeof(char): 1
sizeof(short): 2
sizeof(int): 4
sizeof(long): 4
sizeof(long long): 8
[64-bit Windows]
C:\Users\me\Downloads>sizes64.exe
sizeof(char): 1
sizeof(short): 2
sizeof(int): 4
sizeof(long): 4
sizeof(long long): 8
long
必须至少为 32 位,至少与 int
一样大且不大于 long long
。而已。期间.
向后兼容!
Windows 来自 sizeof(long) == 4
的 16 位平台,它广泛使用自定义类型,如 LONG
、DWORD
... 21=]。微软对向后兼容性采取非常严肃的立场(有时甚至 modifying its code to make stupid old code work),改变这一点会产生很多问题
Over on Channel 9, member Beer28 wrote, "I can't imagine there are too many problems with programs that have type widths changed." I got a good chuckle out of that and made a note to write up an entry on the Win64 data model.
The Win64 team selected the LLP64 data model, in which all integral types remain 32-bit values and only pointers expand to 64-bit values. Why?
In addition to the reasons give on that web page, another reason is that doing so avoids breaking persistence formats. For example, part of the header data for a bitmap file is defined by the following structure:
typedef struct tagBITMAPINFOHEADER {
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, FAR *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;
If a LONG expanded from a 32-bit value to a 64-bit value, it would not be possible for a 64-bit program to use this structure to parse a bitmap file.
您已经收到了很多有效回复。
仅作记录,这里是C++标准中的精确定义:
3.9.1/2: There are five standard signed integer types : “signed char”, “short
int”, “int”, “long int”, and “long long int”. In this list, each type
provides at least as much storage as those preceding it in the list.
(...) Plain ints have the natural size suggested by the architecture
of the execution environment (44).
最后一句暗示int是寄存器对应的大小。不幸的是,它并没有讲述宇宙起源的全部故事,它的脚注只是说:“(44) 即大到足以包含 INT_MIN 和 INT_MAX,定义在 header <climits>
"
有人能告诉我这有什么意义,以及如何让它停止吗?说真的,我是疯了还是 64 位 Windows 长类型只有 4 个字节?这有什么意义?我认为本机 long primitive 大小应该与本机寄存器大小相同。
[32-bit Linux]
me@u32:~$ ./sizes32
sizeof(char): 1
sizeof(short): 2
sizeof(int): 4
sizeof(long): 4
sizeof(long long): 8
[64-bit Linux]
me@u64:~$ ./sizes64
sizeof(char): 1
sizeof(short): 2
sizeof(int): 4
sizeof(long): 8
sizeof(long long): 8
[32-bit Windows]
C:\Users\me\Downloads>sizes32.exe
sizeof(char): 1
sizeof(short): 2
sizeof(int): 4
sizeof(long): 4
sizeof(long long): 8
[64-bit Windows]
C:\Users\me\Downloads>sizes64.exe
sizeof(char): 1
sizeof(short): 2
sizeof(int): 4
sizeof(long): 4
sizeof(long long): 8
long
必须至少为 32 位,至少与 int
一样大且不大于 long long
。而已。期间.
向后兼容!
Windows 来自 sizeof(long) == 4
的 16 位平台,它广泛使用自定义类型,如 LONG
、DWORD
... 21=]。微软对向后兼容性采取非常严肃的立场(有时甚至 modifying its code to make stupid old code work),改变这一点会产生很多问题
Over on Channel 9, member Beer28 wrote, "I can't imagine there are too many problems with programs that have type widths changed." I got a good chuckle out of that and made a note to write up an entry on the Win64 data model.
The Win64 team selected the LLP64 data model, in which all integral types remain 32-bit values and only pointers expand to 64-bit values. Why?
In addition to the reasons give on that web page, another reason is that doing so avoids breaking persistence formats. For example, part of the header data for a bitmap file is defined by the following structure:
typedef struct tagBITMAPINFOHEADER { DWORD biSize; LONG biWidth; LONG biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; } BITMAPINFOHEADER, FAR *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;
If a LONG expanded from a 32-bit value to a 64-bit value, it would not be possible for a 64-bit program to use this structure to parse a bitmap file.
您已经收到了很多有效回复。
仅作记录,这里是C++标准中的精确定义:
3.9.1/2: There are five standard signed integer types : “signed char”, “short int”, “int”, “long int”, and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list. (...) Plain ints have the natural size suggested by the architecture of the execution environment (44).
最后一句暗示int是寄存器对应的大小。不幸的是,它并没有讲述宇宙起源的全部故事,它的脚注只是说:“(44) 即大到足以包含 INT_MIN 和 INT_MAX,定义在 header <climits>
"