Friday, September 25, 2009

The absolute directive

This post is the about Delphi's absolute directive, according to Delphi 7 Help fileYou 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[32];
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

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!!

5 comments:

"Absolute" in this case should be less efficient than straight-forward masking and shifting, as sharing the address in memory forces the parameter to be stored on the stack, rather than leaving it in a register.

I should clarify that my comment applies to extracting the bytes out of an integer, not the str_bytes approach - which I don't recommend, treating managed types like strings and dynamic arrays as equivalent at the bit-level can lead to surprising results (e.g. exceptions, memory corruption) quite easily, if you e.g. make copies of them, or bounds-checked indexing (string stores length as count of chars, while array stores count of elements, but sizeof(char) != sizeof(byte)).

Thank you for clarifying.This is just a simple example of using the "absolute" directive, I couldn't think of anything else that would help beginners understand it, if you wish to correct me on anything please do so.