基本的Delphi类型如何相互关联?

时间:2021-03-19 19:40:12

Delphi has long supported a few basic numeric types and I was wondering how they are related to each other.

Delphi长期以来支持一些基本的数字类型,我想知道它们是如何相互关联的。

In Delphi 2007 I found these declarations (some are conflicting, some are mere aliasses) :

在Delphi 2007中,我发现了这些声明(有些是冲突的,有些只是别名):

Types.pas:

DWORD = LongWord;
Largeint = Int64;

getmem.inc:

DWORD = Integer;

Windows.pas:

DWORD = Types.DWORD;
SHORT = Smallint;
UINT = LongWord;
ULONG = Cardinal;
LONGLONG = Int64;
TLargeInteger = Int64;
ULONGLONG = UInt64;

This leads me into thinking the base signed numeric types are SmallInt, Integer and Int64. Unsigned, there's Byte, WORD and UInt64. But what is the difference between Cardinal and LongWord? (By the way, what's the original and intended casing for these types?)

这让我想到基本签名数字类型是SmallInt,Integer和Int64。无符号,有Byte,WORD和UInt64。但Cardinal和LongWord有什么区别? (顺便说一下,这些类型的原始和预期外壳是什么?)

And is there a type for signed 8 bit integers (Int8)?

是否有一个有符号8位整数(Int8)的类型?

// Int8 = ?unknown?;
UInt8 = Byte;
Int16 = SmallInt;
UInt16 = Word;
Int32 = Integer;
UInt32 = LongWord;
// Int64 already exists
// UInt64 already exists

Lastly, how should I define Int and UInt, especially with regard to C/C++ compatibility and a future switch to other platforms (possibly also 64 bit)? (A related question is, of course, how will the various numeric types be defined in 64-bit Delphi?)

最后,我应该如何定义Int和UInt,特别是关于C / C ++兼容性以及将来切换到其他平台(可能还有64位)? (当然,一个相关的问题是如何在64位Delphi中定义各种数字类型?)

4 个解决方案

#1


The signed one-byte integer type is ShortInt. You can remember its size by the fact that it's not the same size as usual C implementations of the short type.

带符号的一字节整数类型是ShortInt。您可以通过它与短类型的常规C实现的大小不同来记住它的大小。

As for capitalization, capitalize the first letter. The documentation tends to leave the "int" part at the end lowercase, as in Longint, but I think it's more common to capitalize it. Don't write the types in all capitals unless you're using Platform SDK types and you want your code to show its C roots; otherwise I'd just write Word and DWord, Long and ULong, etc.)

至于大写,首字母大写。文档倾向于将“int”部分留在末尾小写,如Longint,但我认为将其大写更常见。除非您使用的是Platform SDK类型,并且希望代码显示其C根,否则不要在所有大写中编写类型;否则我只会写Word和DWord,Long和ULong等)

Delphi 2009, perhaps earlier, already defines types like Int8 and UInt32. As for how to define Int and UInt, I'd say don't. The language you're using already defines Integer and Cardinal; don't introduce new type names when you don't have to. Keep the names you already have, and then everyone else will know what you're talking about. (Besides, Int is already a function in the System unit.)

Delphi 2009,也许更早,已经定义了类似Int8和UInt32的类型。至于如何定义Int和UInt,我会说没有。您正在使用的语言已经定义了Integer和Cardinal;不必要时不要引入新的类型名称。保留你已有的名字,然后其他人都会知道你在说什么。 (此外,Int已经是系统单元中的一个功能。)

Use Cardinal when you want an unsigned type and don't care about its size; use LongWord when the variable must be exactly four bytes. Likewise for Integer and LongInt. Use Cardinal when you want a four-byte unsigned type; use LongWord when you want a generic unsigned type and don't care about the size. Likewise for Integer and LongInt, nowadays. If you're writing 16-bit code, use LongInt when you need four bytes and use Integer when you don't care about the size; Cardinal and LongWord didn't exist in Delphi's and Turbo Pascal's 16-bit days.

当你想要一个无符号类型并且不关心它的大小时,请使用Cardinal;当变量必须恰好是四个字节时使用LongWord。同样适用于Integer和LongInt。如果需要四字节无符号类型,请使用Cardinal;如果需要通用的无符号类型而不关心大小,请使用LongWord。同样对于Integer和LongInt来说,如今。如果您正在编写16位代码,请在需要四个字节时使用LongInt,在不关心大小时使用Integer; Cardinal和LongWord在Delphi和Turbo Pascal的16位时代并不存在。

The common wisdom for years had been that Integer and Cardinal would become 64-bit types on a 64-bit compiler, but that is apparently not the case. Instead, they will remain 32-bit types, just as their counterparts in Microsoft C++ do. Furthermore, there will be a new type, NativeInt, which will be a 64-bit type in a 64-bit compiler. The LongInt and LongWord types will become 64-bit types because they have always been the same size as the Pointer type, which was 32 bits even in 16-bit times.

多年来的常识是,Integer和Cardinal将在64位编译器上成为64位类型,但显然并非如此。相反,它们将保持32位类型,就像它们在Microsoft C ++中的对应物一样。此外,将有一个新类型NativeInt,它将是64位编译器中的64位类型。 LongInt和LongWord类型将成为64位类型,因为它们始终与指针类型相同,即使在16位时也是32位。

#2


UInt8 = Byte
Int8 = ShortInt
UInt16 = Word
Int16 = SmallInt
UInt32 = LongWord
Int32 = LongInt
UInt64 = UInt64
Int64 = Int64

int = Integer
uint = Cardinal

NativeInt (generic, depends on CPU register size)
NativeUInt (generic, depends on CPU register size)

Cardinal and Integer are generic types. For 16 bit they were 16 byte large and for 32 bit they are 32 bit large. For 64 bit the Windows 64bit platform (LLP64) defines them as 32 bit. The new NativeInt and NativeUInt types are now the CPU register sized types.

Cardinal和Integer是通用类型。对于16位,它们是16字节大,而对于32位,它们是32位大。对于64位,Windows 64位平台(LLP64)将它们定义为32位。新的NativeInt和NativeUInt类型现在是CPU寄存器大小的类型。

#3


Cardinal and Integer are aliases.

Cardinal和Integer是别名。

Cardinal ==> LongWord  (unsigned)
Integer  ==> LongInt   (signed)

http://i38.tinypic.com/9s5ufc.jpg

#4


To get "the original and intended casing" press Ctrl-Space, Return after you typed a type name (i.e. use code completion).

要获得“原始和预期的外壳”按Ctrl-空格键,请在键入类型名称后返回(即使用代码完成)。

#1


The signed one-byte integer type is ShortInt. You can remember its size by the fact that it's not the same size as usual C implementations of the short type.

带符号的一字节整数类型是ShortInt。您可以通过它与短类型的常规C实现的大小不同来记住它的大小。

As for capitalization, capitalize the first letter. The documentation tends to leave the "int" part at the end lowercase, as in Longint, but I think it's more common to capitalize it. Don't write the types in all capitals unless you're using Platform SDK types and you want your code to show its C roots; otherwise I'd just write Word and DWord, Long and ULong, etc.)

至于大写,首字母大写。文档倾向于将“int”部分留在末尾小写,如Longint,但我认为将其大写更常见。除非您使用的是Platform SDK类型,并且希望代码显示其C根,否则不要在所有大写中编写类型;否则我只会写Word和DWord,Long和ULong等)

Delphi 2009, perhaps earlier, already defines types like Int8 and UInt32. As for how to define Int and UInt, I'd say don't. The language you're using already defines Integer and Cardinal; don't introduce new type names when you don't have to. Keep the names you already have, and then everyone else will know what you're talking about. (Besides, Int is already a function in the System unit.)

Delphi 2009,也许更早,已经定义了类似Int8和UInt32的类型。至于如何定义Int和UInt,我会说没有。您正在使用的语言已经定义了Integer和Cardinal;不必要时不要引入新的类型名称。保留你已有的名字,然后其他人都会知道你在说什么。 (此外,Int已经是系统单元中的一个功能。)

Use Cardinal when you want an unsigned type and don't care about its size; use LongWord when the variable must be exactly four bytes. Likewise for Integer and LongInt. Use Cardinal when you want a four-byte unsigned type; use LongWord when you want a generic unsigned type and don't care about the size. Likewise for Integer and LongInt, nowadays. If you're writing 16-bit code, use LongInt when you need four bytes and use Integer when you don't care about the size; Cardinal and LongWord didn't exist in Delphi's and Turbo Pascal's 16-bit days.

当你想要一个无符号类型并且不关心它的大小时,请使用Cardinal;当变量必须恰好是四个字节时使用LongWord。同样适用于Integer和LongInt。如果需要四字节无符号类型,请使用Cardinal;如果需要通用的无符号类型而不关心大小,请使用LongWord。同样对于Integer和LongInt来说,如今。如果您正在编写16位代码,请在需要四个字节时使用LongInt,在不关心大小时使用Integer; Cardinal和LongWord在Delphi和Turbo Pascal的16位时代并不存在。

The common wisdom for years had been that Integer and Cardinal would become 64-bit types on a 64-bit compiler, but that is apparently not the case. Instead, they will remain 32-bit types, just as their counterparts in Microsoft C++ do. Furthermore, there will be a new type, NativeInt, which will be a 64-bit type in a 64-bit compiler. The LongInt and LongWord types will become 64-bit types because they have always been the same size as the Pointer type, which was 32 bits even in 16-bit times.

多年来的常识是,Integer和Cardinal将在64位编译器上成为64位类型,但显然并非如此。相反,它们将保持32位类型,就像它们在Microsoft C ++中的对应物一样。此外,将有一个新类型NativeInt,它将是64位编译器中的64位类型。 LongInt和LongWord类型将成为64位类型,因为它们始终与指针类型相同,即使在16位时也是32位。

#2


UInt8 = Byte
Int8 = ShortInt
UInt16 = Word
Int16 = SmallInt
UInt32 = LongWord
Int32 = LongInt
UInt64 = UInt64
Int64 = Int64

int = Integer
uint = Cardinal

NativeInt (generic, depends on CPU register size)
NativeUInt (generic, depends on CPU register size)

Cardinal and Integer are generic types. For 16 bit they were 16 byte large and for 32 bit they are 32 bit large. For 64 bit the Windows 64bit platform (LLP64) defines them as 32 bit. The new NativeInt and NativeUInt types are now the CPU register sized types.

Cardinal和Integer是通用类型。对于16位,它们是16字节大,而对于32位,它们是32位大。对于64位,Windows 64位平台(LLP64)将它们定义为32位。新的NativeInt和NativeUInt类型现在是CPU寄存器大小的类型。

#3


Cardinal and Integer are aliases.

Cardinal和Integer是别名。

Cardinal ==> LongWord  (unsigned)
Integer  ==> LongInt   (signed)

http://i38.tinypic.com/9s5ufc.jpg

#4


To get "the original and intended casing" press Ctrl-Space, Return after you typed a type name (i.e. use code completion).

要获得“原始和预期的外壳”按Ctrl-空格键,请在键入类型名称后返回(即使用代码完成)。