The difference between LongInt and Integer, LongWord and Cardinal

In Delphi, what is the difference between LongInt and Integer, LongWord and Cardinal?

and sometimes I find using DWORD, what is it?

Are they consistent across all versions of Delphi? what should i stick to?

+6
source share
5 answers

In short: Longint and Longword are fixed integers, the former are signed, the latter are unsigned and both are usually 32 bits. Their size depends on the platform in XE8, but a fixed size (32 bits) in XE7 and earlier versions.

The whole and cardinal are not fixed. These are the so-called "common" integers (do not confuse this with generics, which are another pair of shoes), i.e. They should preferably be used when an integral type is required, regardless of size. Depending on the version and platform, the size of Integer and Cardinal may vary. They currently have the same size and type as Longint and Longword.

Sizes of fixed sizes do not differ between versions or platforms. You should use these types where you should interact with code or data from sources other than your own program, in other words, where exact binary compatibility is important, for example. when calling API functions. Consequently, the use of types such as DWORD, etc.

Please note that current versions have aliases for types such as Byte or Smallint. These are Int8, UInt8, Int16, UInt16, etc. Before UInt64. ISTM that these names are easier to remember than, for example, “Smallint” (16 bits signed) or “Shortint” (8 bits signed).

Therefore, use Integer and Cardinal whenever possible, as they are probably ideal types for the platform and version. Use fixed-size types, such as Byte, Smallint, Longint, or UInt64, just to name a few when exact binary compatibility with other data is required.

+7
source
  • Delphi Integer is a basic C ++ int platform.
  • Delphi LongInt is the base platform of C ++ long int .
  • Delphi Cardinal is the base platform of C ++ unsigned int .
  • Delphi LongWord is the base platform of C ++ unsigned long int .

All four of these types are platform dependent.

On all supported platforms, Integer and Cardinal are 32-bit types during recording. Types are platform dependent, so on all supported platforms the types are the same size.

On 64-bit * nix platforms, LongInt and LongWord are 64-bit types. On all other supported platforms, at the time of writing, types are 32-bit types.

The key point is that these types are platform dependent.

DWORD is the type alias used by the Windows API. Use it only when using this API.

Should you use Integer or LongInt ? It depends on your use. As a rule, a type corresponding to the type of C ++ code is used for interaction. Otherwise, Integer used for most purposes. Of course, this is a general answer to your general question.

+3
source

I suggest you check out the Delphi documentation for a better explanation: http://docwiki.embarcadero.com/RADStudio/XE5/en/Delphi_Data_Types

+2
source

Integer is a 32-bit signed integer type - Longint is an alias for this type. Cardinal is a 32-bit unsigned integer type - LongWord and DWORD are aliases for this type.

-1
source

Previously, there were fundamental integer types (should never change) and generic types , which could theoretically differ for different platforms, although integer types never changed. Now in the documentation for XE6 there is a more logical, simple and clear definition of integer types and only two platform-dependent integer types:

 NativeInt NativeUInt 

All other integer types are platform independent:

 ShortInt SmallInt LongInt Integer Int64 Byte Word LongWord Cardinal UInt64 

See the Simple Types section of the Help for more information.

-1
source

Source: https://habr.com/ru/post/971587/


All Articles