简体   繁体   English

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

[英]Why is the size of a native long primitive on 64-bit Windows only 4 bytes?

Will someone please tell me how this makes any sense, and how to make it stop?有人会告诉我这有什么意义,以及如何让它停止? Seriously, am I crazy or is the 64-bit Windows long type only 4 bytes?说真的,我是疯了还是 64 位 Windows 长类型只有 4 个字节? How does that make any sense?这有什么意义? I thought the native long primitive size was supposed to be the same as the native register size.我认为本机长原始大小应该与本机寄存器大小相同。

[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

Backward compatibility!向后兼容!

Windows came from a 16-bit platform where sizeof(long) == 4 and it makes extensive use of custom types like LONG , DWORD ... in its API. Windows 来自一个 16 位平台,其中sizeof(long) == 4并且它在其 API 中广泛使用了LONGDWORD ... 等自定义类型。 Microsoft takes a very serious stance on backward compatibility (sometimes evenmodifying its code to make stupid old code work ) and changing that would make a lot of issues Microsoft 对向后兼容性采取了非常严肃的立场(有时甚至修改其代码以使愚蠢的旧代码工作)并且更改会产生很多问题

Over on Channel 9, member Beer28 wrote, "I can't imagine there are too many problems with programs that have type widths changed."在第 9 频道,成员 Beer28 写道:“我无法想象更改类型宽度的程序存在太多问题。” I got a good chuckle out of that and made a note to write up an entry on the Win64 data model.我从中得到了一个很好的笑声,并做了一个笔记来写一篇关于 Win64 数据模型的条目。

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. Win64 团队选择了 LLP64 数据模型,其中所有整数类型保持 32 位值,只有指针扩展为 64 位值。 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.如果 LONG 从 32 位值扩展为 64 位值,则 64 位程序将无法使用此结构来解析位图文件。

Why did the Win64 team choose the LLP64 model? Win64 团队为什么选择 LLP64 模型?

long has to be at least 32-bits, at least as big as int and no bigger than long long . long必须至少为 32 位,至少与int一样大且不大于long long That's it.就是这样。 Period.时期。

You got already plenty of valid responses.您已经收到了大量有效回复。

Just for the records, here the precise definition in the C++ standard:只是为了记录,这里是 C++ 标准中的精确定义:

3.9.1/2: There are five standard signed integer types : “signed char”, “short int”, “int”, “long int”, and “long long int”. 3.9.1/2:有五种标准的有符号整数类型:“signed char”、“short int”、“int”、“long int”和“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). (...) 普通整数具有执行环境架构建议的自然大小 (44)。

The last sentence suggests that int is of the size corresponding to the register.最后一句暗示 int 的大小与寄存器对应。 Unfortunately, rather than telling the full story of the origin of the universe, its footnote just says: " (44) that is, large enough to contain any value in the range of INT_MIN and INT_MAX, as defined in the header <climits> "不幸的是,它没有讲述宇宙起源的完整故事,它的脚注只是说:“ (44) 也就是说,足够大以包含 INT_MIN 和 INT_MAX 范围内的任何值,如标题<climits>所定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM