Cardinal

From Free Pascal wiki
Revision as of 00:00, 10 February 2013 by B4Z1l3 (talk | contribs)
Jump to navigationJump to search

Cardinal is an integer type defined as an alias for DWord under a 32bit plateform. Like the DWord (double word) type it's a 32 bit data interpreted as an unsigned integer. As it's only used on 32 bit systems, its minimal value is 0x0000000 and its maximal value 0xFFFFFFFF.

On the 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 the 32 and 64 bits little endian systems, using the Cardinal type is not recommended anymore for memory/pointer operations/arithmetic. Actually it's rather recommended to use NativeInt or NativeUInt types. These types will match to a CPU register wide data which can be used to encode a memory address and so will be always valid. For example under a 64bit 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.