Difference between revisions of "Cardinal"

From Lazarus wiki
Jump to navigationJump to search
m (Fixed syntax highlighting)
 
Line 1: Line 1:
 
{{Cardinal}}
 
{{Cardinal}}
  
Cardinal is an integer type defined as an alias for DWord under a 32-bit platform. Like the DWord (double word) type it's 32 bits and interpreted as an unsigned integer. Its minimal value is 0x0000000 and its maximal value 0xFFFFFFFF (4,294,967,295).
+
'''Cardinal''' is an integer type defined as an alias for [[DWord]] under a 32-bit platform. Like the DWord (double word) type it's 32 bits and interpreted as an unsigned integer. Its minimal value is 0x0000000 and its maximal value 0xFFFFFFFF (4,294,967,295).
  
 
On x86 systems Cardinal type is often used to hold a memory address, like a pointer:
 
On x86 systems Cardinal type is often used to hold a memory address, like a pointer:
Line 18: Line 18:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
However, because of 32 and 64 bit little endian systems, using the Cardinal type is not recommended anymore for memory/pointer operations/arithmetic.
+
However, because of 32 and 64 bit little endian systems, using the Cardinal type is '''''not recommended''''' anymore for memory/pointer operations/arithmetic.
 
It's recommended to use '''NativeInt''' or '''NativeUInt''' types instead. These types will match the width of the CPU registers which can be used to encode a memory address and so will always be the right size. For example under a 64b-bit OS, a NativeUInt will be like a UInt64 or a QuadWord and under a 32-bit OS, a NativeUInt will be like a DWord or a Cardinal.
 
It's recommended to use '''NativeInt''' or '''NativeUInt''' types instead. These types will match the width of the CPU registers which can be used to encode a memory address and so will always be the right size. For example under a 64b-bit OS, a NativeUInt will be like a UInt64 or a QuadWord and under a 32-bit OS, a NativeUInt will be like a DWord or a Cardinal.
  
 
{{Data types}}
 
{{Data types}}

Latest revision as of 12:40, 24 November 2020

Deutsch (de) English (en) français (fr) русский (ru)

Cardinal is an integer type defined as an alias for DWord under a 32-bit platform. Like the DWord (double word) type it's 32 bits and interpreted as an unsigned integer. Its minimal value is 0x0000000 and its maximal value 0xFFFFFFFF (4,294,967,295).

On x86 systems Cardinal type is often used to hold a memory address, like a pointer:

  var
    anAddress: Cardinal;
    anObject: TObject;
  begin
    anAddress := Cardinal(Self);
    with TObject(anAddress) do
    begin
      // anAddress is casted as a TObject.
    end;
  end;

However, because of 32 and 64 bit little endian systems, using the Cardinal type is not recommended anymore for memory/pointer operations/arithmetic. It's recommended to use NativeInt or NativeUInt types instead. These types will match the width of the CPU registers which can be used to encode a memory address and so will always be the right size. For example under a 64b-bit OS, a NativeUInt will be like a UInt64 or a QuadWord and under a 32-bit OS, a NativeUInt will be like a DWord or a Cardinal.


navigation bar: data types
simple data types

boolean byte cardinal char currency double dword extended int8 int16 int32 int64 integer longint real shortint single smallint pointer qword word

complex data types

array class object record set string shortstring