C++ 64 位 Windows 上 long 的位大小是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/384502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the bit size of long on 64-bit Windows?
提问by Jonathan Leffler
Not to long ago, someone told me that long
are not 64 bits on 64 bit machines and I should always use int
. This did not make sense to me. I have seen docs (such as the one on Apple's official site) say that long
are indeed 64 bits when compiling for a 64-bit CPU. I looked up what it was on 64-bit Windows and found
不久前,有人告诉我long
64 位机器上不是 64 位,我应该始终使用int
. 这对我来说没有意义。我看过文档(例如 Apple 官方网站上的文档)说long
在为 64 位 CPU 编译时确实是 64 位。我在 64 位 Windows 上查找它是什么,发现
- Windows:
long
andint
remain 32-bit in length, and special new data types are defined for 64-bit integers.
- Windows:
long
并int
保持 32 位长度,并且为 64 位整数定义了特殊的新数据类型。
(from http://www.intel.com/cd/ids/developer/asmo-na/eng/197664.htm?page=2)
(来自http://www.intel.com/cd/ids/developer/asmo-na/eng/197664.htm?page=2)
What should I use? Should I define something like uw
, sw
((un)signed width) as a long
if not on Windows, and otherwise do a check on the target CPU bitsize?
我应该使用什么?我是否应该将uw
, sw
((un)signed width) 之类的内容定义为long
if 不在 Windows 上,否则检查目标 CPU 位大小?
回答by Jonathan Leffler
In the Unix world, there were a few possible arrangements for the sizes of integers and pointers for 64-bit platforms. The two mostly widely used were ILP64 (actually, only a very few examples of this; Cray was one such) and LP64 (for almost everything else). The acronynms come from 'int, long, pointers are 64-bit' and 'long, pointers are 64-bit'.
在 Unix 世界中,对于 64 位平台的整数和指针的大小有几种可能的安排。最广泛使用的两个是 ILP64(实际上,只有很少的例子;Cray 就是这样的一个)和 LP64(几乎所有其他的东西)。首字母缩略词来自“int, long,pointers are 64-bit”和“long,pointers are 64-bit”。
Type ILP64 LP64 LLP64
char 8 8 8
short 16 16 16
int 64 32 32
long 64 64 32
long long 64 64 64
pointer 64 64 64
The ILP64 system was abandoned in favour of LP64(that is, almost all later entrants used LP64, based on the recommendations of the Aspen group; only systems with a long heritage of 64-bit operation use a different scheme). All modern 64-bit Unix systems use LP64. MacOS X and Linux are both modern 64-bit systems.
ILP64 系统被LP64所取代(也就是说,根据 Aspen 小组的建议,几乎所有后来的进入者都使用 LP64;只有具有 64 位操作的悠久传统的系统才使用不同的方案)。所有现代 64 位 Unix 系统都使用 LP64。MacOS X 和 Linux 都是现代 64 位系统。
Microsoft uses a different scheme for transitioning to 64-bit: LLP64 ('long long, pointers are 64-bit'). This has the merit of meaning that 32-bit software can be recompiled without change. It has the demerit of being different from what everyone else does, and also requires code to be revised to exploit 64-bit capacities. There always was revision necessary; it was just a different set of revisions from the ones needed on Unix platforms.
Microsoft 使用不同的方案来转换到 64 位:LLP64('long long,pointers are 64-bit')。这意味着 32 位软件无需更改即可重新编译。它的缺点是与其他人所做的不同,并且还需要修改代码以利用 64 位容量。总是需要修改;它只是与 Unix 平台上所需的一组不同的修订。
If you design your software around platform-neutral integer type names, probably using the C99 <inttypes.h>
header, which, when the types are available on the platform, provides, in signed (listed) and unsigned (not listed; prefix with 'u'):
如果您围绕与平台无关的整数类型名称设计软件,则可能使用 C99<inttypes.h>
标头,当平台上的类型可用时,该标头提供有符号(已列出)和无符号(未列出;前缀为“u”):
int8_t
- 8-bit integersint16_t
- 16-bit integersint32_t
- 32-bit integersint64_t
- 64-bit integersuintptr_t
- unsigned integers big enough to hold pointersintmax_t
- biggest size of integer on the platform (might be larger thanint64_t
)
int8_t
- 8 位整数int16_t
- 16 位整数int32_t
- 32 位整数int64_t
- 64 位整数uintptr_t
- 足以容纳指针的无符号整数intmax_t
- 平台上整数的最大大小(可能大于int64_t
)
You can then code your application using these types where it matters, and being very careful with system types (which might be different). There is an intptr_t
type - a signed integer type for holding pointers; you should plan on not using it, or only using it as the result of a subtraction of two uintptr_t
values (ptrdiff_t
).
然后,您可以在重要的地方使用这些类型对您的应用程序进行编码,并对系统类型(可能不同)非常小心。有一种intptr_t
类型——用于保存指针的有符号整数类型;您应该计划不使用它,或者仅将其用作两个uintptr_t
值相减的结果( ptrdiff_t
)。
But, as the question points out (in disbelief), there are different systems for the sizes of the integer data types on 64-bit machines. Get used to it; the world isn't going to change.
但是,正如问题所指出的(难以置信),64 位机器上整数数据类型的大小有不同的系统。习惯它; 世界不会改变。
回答by Martin Liversage
It is not clear if the question is about the Microsoft C++ compiler or the Windows API. However, there is no [c++] tag so I assume it is about the Windows API. Some of the answers have suffered from link rot so I am providing yet another link that can rot.
目前尚不清楚问题是关于 Microsoft C++ 编译器还是 Windows API。但是,没有 [c++] 标签,所以我认为它是关于 Windows API 的。一些答案受到链接腐烂的影响,所以我提供了另一个可能腐烂的链接。
For information about Windows API types like INT
, LONG
etc. there is a page on MSDN:
有关Windows API类型,如信息INT
,LONG
等有MSDN上的页面:
The information is also available in various Windows header files like WinDef.h
. I have listed a few relevant types here:
这些信息也可以在各种 Windows 头文件中找到,例如WinDef.h
. 我在这里列出了一些相关的类型:
Type | S/U | x86 | x64 ----------------------------+-----+--------+------- BYTE, BOOLEAN | U | 8 bit | 8 bit ----------------------------+-----+--------+------- SHORT | S | 16 bit | 16 bit USHORT, WORD | U | 16 bit | 16 bit ----------------------------+-----+--------+------- INT, LONG | S | 32 bit | 32 bit UINT, ULONG, DWORD | U | 32 bit | 32 bit ----------------------------+-----+--------+------- INT_PTR, LONG_PTR, LPARAM | S | 32 bit | 64 bit UINT_PTR, ULONG_PTR, WPARAM | U | 32 bit | 64 bit ----------------------------+-----+--------+------- LONGLONG | S | 64 bit | 64 bit ULONGLONG, QWORD | U | 64 bit | 64 bit
The column "S/U" denotes signed/unsigned.
“S/U”列表示有符号/无符号。
回答by Mark Ransom
Microsoft has also defined UINT_PTR and INT_PTR for integers that are the same size as a pointer.
Microsoft 还为与指针大小相同的整数定义了 UINT_PTR 和 INT_PTR。
Here is a list of Microsoft specific types- it's part of their driver reference, but I believe it's valid for general programming as well.
这是Microsoft 特定类型的列表- 它是其驱动程序参考的一部分,但我相信它也适用于一般编程。
回答by reuben
This article on MSDN references a number of type aliases (available on Windows) that are a bit more explicit with respect to their width:
MSDN 上的这篇文章引用了许多类型别名(在 Windows 上可用),它们在宽度方面更加明确:
http://msdn.microsoft.com/en-us/library/aa505945.aspx
http://msdn.microsoft.com/en-us/library/aa505945.aspx
For instance, although you can use ULONGLONG to reference a 64-bit unsigned integral value, you can also use UINT64. (The same goes for ULONG and UINT32.) Perhaps these will be a bit clearer?
例如,虽然您可以使用 ULONGLONG 来引用 64 位无符号整数值,但您也可以使用 UINT64。(ULONG 和 UINT32 也是如此。)也许这些会更清楚一些?
回答by Paul de Vrieze
The easiest way to get to know it for your compiler/platform:
为您的编译器/平台了解它的最简单方法:
#include <iostream>
int main() {
std::cout << sizeof(long)*8 << std::endl;
}
Themultiplication by 8 is to get bits from bytes.
乘以 8 是从字节中获取位。
When you need a particular size, it is often easiest to use one of the predefined types of a library. If that is undesirable, you can do what often happens with autoconf software and have the configuration system determine the right type for the needed size.
当您需要特定大小时,通常最容易使用库的一种预定义类型。如果这是不合需要的,您可以执行 autoconf 软件经常发生的事情,并让配置系统确定所需大小的正确类型。
回答by BeeOnRope
The size in bits of long
on Windows platforms is 32 bits (4 bytes).
long
Windows 平台上的位大小为 32 位(4 字节)。
You can check this using sizeof(long)
.
您可以使用sizeof(long)
.
回答by PolyThinker
If you need to use integers of certain length, you probably should use some platform independent headers to help you. Boost is a good place to look at.
如果您需要使用特定长度的整数,您可能应该使用一些与平台无关的标头来帮助您。Boost 是一个值得关注的好地方。