在 64 位 Windows 上 long 的位大小是多少? [英] What is the bit size of long on 64-bit Windows?

查看:29
本文介绍了在 64 位 Windows 上 long 的位大小是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不久前,有人告诉我 long 在 64 位机器上不是 64 位,我应该始终使用 int.这对我来说没有意义.我看过文档(例如 Apple 官方网站上的文档)说在为 64 位 CPU 编译时,long 确实是 64 位.我在 64 位 Windows 上查找了它,发现

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

  • Windows:longint 保持 32 位长度,以及特殊的新数据类型为 64 位整数定义.
  • Windows: long and int remain 32-bit in length, and special new data types are defined for 64-bit integers.

(来自 http://www.intel.com/cd/ids/developer/asmo-na/eng/197664.htm?page=2)

我应该使用什么?如果不在 Windows 上,我是否应该将 uwsw ((un)signed width) 定义为 long,否则请检查目标 CPU 位大小?

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?

推荐答案

在 Unix 世界中,对于 64 位平台,整数和指针的大小有几种可能的安排.最广泛使用的两个是 ILP64(实际上,只有极少数的例子;Cray 就是这样一个例子)和 LP64(几乎所有其他的例子).缩写词来自int, long, pointers are 64-bit"和long, pointers are 64-bit".

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'.

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

ILP64 系统被 LP64 所取代(也就是说,几乎根据 Aspen 小组的建议,所有后来的参赛者都使用 LP64;只有具有 64 位操作传统的系统才使用不同的方案).所有现代 64 位 Unix 系统都使用 LP64.MacOS X 和 Linux 都是现代 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.

Microsoft 使用不同的方案转换到 64 位:LLP64('long long,指针是 64 位').这意味着 32 位软件可以在不改变的情况下重新编译.它的缺点是与其他人所做的不同,并且还需要修改代码以利用 64 位容量.总是需要修改;它只是一组与 Unix 平台所需的不同的修订.

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.

如果您围绕平台中立的整数类型名称设计软件,可能使用 C99 标头,当类型在平台上可用时,该标头提供带符号的(列出)和无符号(未列出;前缀为 'u'):

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'):

  • int8_t - 8 位整数
  • int16_t - 16 位整数
  • int32_t - 32 位整数
  • int64_t - 64 位整数
  • uintptr_t - 大到足以容纳指针的无符号整数
  • intmax_t - 平台上最大整数大小(可能大于int64_t)
  • int8_t - 8-bit integers
  • int16_t - 16-bit integers
  • int32_t - 32-bit integers
  • int64_t - 64-bit integers
  • uintptr_t - unsigned integers big enough to hold pointers
  • intmax_t - biggest size of integer on the platform (might be larger than int64_t)

然后,您可以在重要的地方使用这些类型对您的应用程序进行编码,并对系统类型(可能不同)非常小心.有一个 intptr_t 类型——一个用于保存指针的有符号整数类型;您应该计划不使用它,或者仅将其用作两个 uintptr_t 值 (ptrdiff_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).

但是,正如问题所指出的那样(难以置信),64 位机器上整数数据类型的大小有不同的系统.习惯它;世界不会改变.

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 位 Windows 上 long 的位大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆