Cardinal

From Free Pascal wiki
Revision as of 23:53, 9 February 2013 by B4Z1l3 (talk | contribs) (Created page with "Cardinal is an integer type defined as an alias for DWord under a 32bit plateform. Like the DWord (double word) it's a 32 bit data interpreted as an unsigned integer. As it's ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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) 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 adress, 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 Cardinals is not recommended anymore for memory/pointer operations/arithmetic. Actually it's recommended to use NativeInt or NativeUInt types. These types will match to a CPU register wide data which can be used to encode the 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.