Difference between revisions of "Cardinal"

From Lazarus wiki
Jump to navigationJump to search
(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 ...")
 
Line 1: Line 1:
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.
+
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 adress, like a pointer:
+
On the x86 systems Cardinal type is often used to hold a memory address, like a pointer:
  
 
<code>
 
<code>
Line 9: Line 9:
 
   begin
 
   begin
 
     AnAddress := Cardinal(Self);
 
     AnAddress := Cardinal(Self);
     With TObject(AnAddress) do
+
     with TObject(AnAddress) do
 
     begin
 
     begin
 
       // AnAddress is casted as a TObject.
 
       // AnAddress is casted as a TObject.
Line 16: Line 16:
 
   </code>
 
   </code>
  
However, because of the 32 and 64 bits little endian systems, using Cardinals is not recommended anymore for memory/pointer operations/arithmetic.
+
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 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.
+
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.

Revision as of 00:00, 10 February 2013

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.