gpt4 book ai didi

c - 为什么 64 位 Windows 上原生 long 原语的大小只有 4 个字节?

转载 作者:可可西里 更新时间:2023-11-01 09:40:44 25 4
gpt4 key购买 nike

有人能告诉我这有什么意义,以及如何让它停止吗?说真的,我是疯了还是 64 位 Windows 长类型只有 4 个字节?这有什么意义?我认为 native long primitive 大小应该与 native 寄存器大小相同。

[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

最佳答案

向后兼容!

Windows 来自 sizeof(long) == 4 的 16 位平台,它广泛使用自定义类型,如 LONGDWORD ...在其API中。 Microsoft 在向后兼容性(有时甚至是 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.

Why did the Win64 team choose the LLP64 model?

关于c - 为什么 64 位 Windows 上原生 long 原语的大小只有 4 个字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33446588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com