You can create a new variable that resides at the same address as another variable. To do so, put the directive absolute after the type name in the declaration of the new variable, followed by the name of an existing (previously declared) variable. For example,
var Str: string; StrLen: Byte absolute Str;
specifies that the variable StrLen should start at the same address as Str. Since the first byte of a short string contains the string's length, the value of StrLen is the length of Str.
You cannot initialize a variable in an absolute declaration or combine absolute with any other directives.
If that makes you dizzy then let's say you want to get a byte from a 32 bit integer(a 32 bit integer is represented on 4 bytes) then with the help of absolute directive we can use this
function getIntByte(thisInteger: Integer; ByteIndex: Byte): Byte; var int_bytes: array[0..3] of Byte absolute thisInteger; begin Result := int_bytes[ByteIndex]; end;
now if we want to get the second byte from the integer we can just use
getIntByte(1), because 0 is the first byte, 1 is the second and so on.
procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(IntToStr(getIntByte(2009, 1))); end;
So... the absolute directive actually gives you access to a variable/parameter's memory for example if you need to get a string as an array of bytes you can simply use
procedure StrAsArrayOfByte(thisString: String); var str_bytes: array of byte absolute thisString: begin (* do something with "str_bytes" *) end;
The possibilities are enormous, you just need to know when to use it, in stead of copying a variable's chars to an array of byte or using Ord(myString[Index]) just use the absolute directive!
Do not hesitate to post comments!!