在Delphi中,LongInt和Integer有什么区别?LongWord和Cardinal有什么区别?
而且我有时会发现使用DWORD,那是什么意思?
它们在所有Delphi版本中都一致吗?我应该坚持使用哪一个?
在Delphi中,LongInt和Integer有什么区别?LongWord和Cardinal有什么区别?
而且我有时会发现使用DWORD,那是什么意思?
它们在所有Delphi版本中都一致吗?我应该坚持使用哪一个?
NativeInt
和NativeUInt
的目的是获得与指针大小相同的整数类型。就这样。如果在将来的某个世界中,64位整数的性能优于32位整数,那么也许Integer
会改变。或者,为了匹配平台内存模型,在64位Mac/Linux上,Longint
将是64位的。我认为你无法预知未来。 - David HeffernanInteger
是底层平台的C++int
。LongInt
是底层平台的C++ long int
。Cardinal
是底层平台的C++ unsigned int
。LongWord
是底层平台的C++ unsigned long int
。这四个类型都是与平台相关的。
在撰写本文时,所有受支持的平台上,Integer
和Cardinal
都是32位类型。尽管这些类型是与平台相关的,但在所有受支持的平台上,它们恰好是相同大小的类型。
在64位*nix平台上,LongInt
和LongWord
是64位类型。在本文撰写时,对于所有其他受支持的平台,这些类型都是32位类型。
关键点是这些类型都是与平台相关的。
DWORD
是Windows API使用的类型别名,只有在使用该API时才使用它。
应该使用Integer
还是LongInt
?这取决于您的使用。一般而言,对于交互操作,请使用与C++代码匹配的任一类型。否则,在大多数情况下,Integer
是适用的。当然,这只是对您一般性问题的一般回答。
我建议您查看 Delphi 文档以获得最佳解释:http://docwiki.embarcadero.com/RADStudio/XE5/en/Delphi_Data_Types
Longint
是一个32位有符号整数。这可能会让某些人误以为Longint
是一个32位有符号整数,但实际上它是一个64位有符号整数。来源:Delphi文档。很令人困惑 - 我知道;因此才有了这个问题。 - Ian BoydLongInt
是 32 位有符号整数,在 64 位 Windows 平台上也是如此,但在 64 位 iOS 平台上是 64 位。这在最新的 Delphi 文档 中仍然成立。此外请注意,在我的回答中,我链接了 Delphi XE5 文档,该文档在发布问题后仅两个月就发布了 Delphi XE6。但现在您正在参考 Delphi XE8 文档,这是一份当时还未出现的 Delphi 版本的文档。 - SilverWarior| Type | 16-bit platform | 32-bit platform | 64-bit platform |
|-------------|-----------------|---------------- |------------------|
| Integer | 16-bit signed | 32-bit signed | "generic type"
| Cardinal | 16-bit unsigned | 32-bit unsigned | "generic type"
| Longint | 32-bit signed | 32-bit signed | 64-bit signed | "fundamental type"
| Longword | n/a | 32-bit unsigned | 64-bit unsigned | "fundamental type"
| Int64 | n/a | 64-bit signed | 64-bit signed | "fundamental type"
| UInt64 | n/a | 64-bit unsigned | 64-bit unsigned | "fundamental type"
| Int32 | n/a | 32-bit signed |
| UInt32 | n/a | 32-bit unsigned |
| NativeInt | n/a | 32-bit signed | 64-bit signed |
| NativeUInt | n/a | 32-bit unsigned | 64-bit unsigned |
| FixedInt | n/a | 32-bit signed |
| FixedUInt | n/a | 32-bit unsigned |
我们都知道他们应该做什么:
| Type | 16-bit platform | 32-bit platform | 64-bit platform |
|-------------|-----------------|---------------- |------------------|
| Integer | 16-bit signed | 32-bit signed | 64-bit signed | "generic type"
| Cardinal | 16-bit unsigned | 32-bit unsigned | 64-bit unsigned | "generic type"
| SmallInt | 16-bit signed | "fundamental type"
| Word | 16-bit unsigned | "fundamental type"
| Longint | 32-bit signed | "fundamental type"
| Longword | 32-bit unsigned | "fundamental type"
| Int64 | 64-bit signed | "fundamental type"
| UInt64 | 64-bit unsigned | "fundamental type"
但他们并没有这样做,所以我们现在的情况就是这样。
可以预期,Int32
一直是 有符号的32位整数;但是保证曾经被打破过。
Integer types
An integer type represents a subset of the whole numbers. The generic integer types are Integer and Cardinal; use these whenever possible, since they result in the best performance for the underlying CPU and operating system. The table below gives their ranges and storage formats for the current 32-bit Object Pascal compiler.
Type Range Format -------- ----------------------- --------------- Integer –2147483648..2147483647 signed 32-bit Cardinal 0..4294967295 unsigned 32-bit
Fundamental integer types include Shortint, Smallint, Longint, Int64, Byte, Word, and Longword.
Type Range Format -------- ----------------------- ---------------- Shortint –128..127 signed 8-bit Smallint –32768..32767 signed 16-bit Longint –2147483648..2147483647 signed 32-bit Int64 –2^63..2^63–1 signed 64-bit Byte 0..255 unsigned 8-bit Word 0..65535 unsigned 16-bit Longword 0..4294967295 unsigned 32-bit
In general, arithmetic operations on integers return a value of type Integer—which, in its current implementation, is equivalent to the 32-bit Longint. Operations return a value of type Int64 only when performed on an Int64 operand.
请注意"在当前实现中,等同于Longint"。当时的想法是Integer
会改变;他们并没有意识到Longint
才会改变。
注意到Integer Types ================
An integer type represents a subset of the integral numbers.
Integer types can be platform-dependent and platform-independent.
Platform-Dependent Integer Types
The platform-dependent integer types are transformed to fit the bit size of the current compiler platform. The platform-dependent integer types are NativeInt and NativeUInt. Using these types whenever possible, since they result in the best performance for the underlying CPU and operating system, is desirable. The following table illustrates their ranges and storage formats for the Delphi compiler.
Platform-dependent integer types
Type Range Format Alias ----------- ----------------------- -------------------------------------- ------------ NativeInt -2147483648..2147483647 Signed 32-bit on 32-bit platforms or Integer -2^63..2^63-1 Signed 64-bit on 64-bit platforms Int64 NativeUInt 0..4294967295 Unsigned 32-bit on 32-bit platforms or Cardinal 0..2^64-1 Unsigned 64-bit on 64-bit platforms UInt64
Platform-Independent Integer Types
Platform-independent integer types always have the same size, regardless of what platform you use. Platform-independent integer types include ShortInt, SmallInt, LongInt, Integer, Int64, Byte, Word, LongWord, Cardinal, and UInt64.
Platform-independent integer types
Type Range Format Alias ----------- ----------------------- --------------- ------ ShortInt -128..127 Signed 8-bit Int8 SmallInt -32768..32767 Signed 16-bit Int16 LongInt -2147483648..2147483647 Signed 32-bit Int32 Integer -2147483648..2147483647 Signed 32-bit Int32 Int64 -2^63..2^63-1 Signed 64-bit Byte 0..255 Unsigned 8-bit UInt8 Word 0..65535 Unsigned 16-bit UInt16 LongWord 0..4294967295 Unsigned 32-bit UInt32 Cardinal 0..4294967295 Unsigned 32-bit UInt32 UInt64 0..2^64-1 Unsigned 64-bit
Integer
和 Cardinal
被记录为平台相关了吗?还注意到 LongInt
和 LongWord
被记录为平台无关了吗?基本
整数类型(不应该改变)和 通用类型
,理论上可以因不同的平台而异,尽管整数类型实际上从未改变。现在,在 XE6 的文档中,整数类型有一个更加逻辑、简单和清晰的定义,只有两种依赖于平台的整数类型:NativeInt
NativeUInt
所有其他整数类型都是与平台无关的:
ShortInt
SmallInt
LongInt
Integer
Int64
Byte
Word
LongWord
Cardinal
UInt64
Integer
在完全错误的列表中。您误解了通用和基本类型的概念。 - Free ConsultingDWORD
是Windows数据类型,而不是别名... - TLamaDWORD
被定义为 Cardinal
的别名。 - David Heffernan