Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


[Discussion] The absolute keyword...

Posted on 2004-08-26
Medium Priority
Last Modified: 2008-01-09
Okay, I guess not many of you have ever heard of this keyword. It might be that I'm actually the only developer who is actually using it anyways. So let's start a discossion about if it's a useful keyword or not...

So, when do I use it? Well, I have a unit that generates log information to a text file. This unit CP_Debug contains this line:

var Log: TextFile;

Plain and simple. The Initialization part creates the file, the finalization parts closes it. In-between I just do lots of WriteLn(Log, ...); to it, thus it generates some interesting information for me to use while debugging. But what I've done in another unit untComponent is put this line of code:

var Log: TextFile absolute CP_Debug.Log;

Now you see how absolute is used. :) Basically, it tells that the variable Log in untComponent is located at the same location as the variable Log in CP_Debug. Which is very interesting but it still doesn't show you the advantage of using this trick.

Well, the advantage is in the third unit I create. In unit frmMain I've this uses clause:

uses untComponent;

And now the big trick... In frmMain I can now use WriteLn(Log, ...); statements that will write to the log file in a unit that is basically invisible for the frmMain unit! I do not have to add the CP_Debug unit to the frmMain unit, keeping the uses clause smaller and thus easier to read. I could even be funnier and add to frmMain this line:

var Log: TextFile absolute untComponent.Log;

And then all units that use frmMain can write to a textfile defined in a faraway unit that they're not even aware of! Well, pretty powerful, isn't it? Basically, it allows you to "forward" global variables to other units.

Okay, now... It's a nice trick but do you consider this a neat trick or would you never even dare to use it? Is it proper to write software like this? The points will go to the one with the best arguments. ;-)
Question by:Wim ten Brink
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1

Assisted Solution

gary_williams earned 120 total points
ID: 11903608

Binding two variables of the same data type via the "absolute" keyword (e.g. var X: Integer absolute Y) doesn't gain you much more abstraction than if you'd simply set the Y variable from the unit that declared X.  All you really gain is the ability to give an alias name to the variable.

I find the "absolute" keyword is more useful when binding different data types.  For example, integer variables can be absolute'd to records of the same size (or vice versa), allowing us to manipulate a portion of the integer, or the entire record, very easily.

An Int64 variable can be bound to a double variable, allowing us to easily express the mantissa and exponent in hex.

Two Int64 variables can be absolute'd to two variables of type TNotifyEvent, allowing us to compare both the class and data pointers within the events via a single comparison.
LVL 11

Accepted Solution

robert_marquardt earned 200 total points
ID: 11903722
I dislike the absolute keyword heavily and is has been banned from the sources of the JVCL.
A variant record can be used to replace it completely. It is also safer because you can do
horrible things with absolute like an overlay of an Integer parameter with an Extended and thus
be able to trash the stack.
LVL 17

Author Comment

by:Wim ten Brink
ID: 11904095
True... The absolute keyword allows some nasty code to be created. But there's a difference between using it to access one memory space as different types of date or, as in my example, as an alias to another unit. I often end up with lots of small units and to keep my uses clause more readable, I do quite a few classtype aliases in my code, so I can create wrapper units around the smaller ones. Better to have "uses wrapper;" than "uses unit1, unit2, unit3, unit4, unit5, unit6;" in my code.
But AFAIK, the absolute keyword can only be used for global variables, not local variables. Trashing the stack is not possible. You could however read garbage but you have the same risk with an invalid typecast... Besides, variant records just provide the same risk factor. You could define something as Integer in one branch and Extended in the other.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 11904595
You can use absolute with two local variables, or a local variable and a procedure argument, with no problem.

How about this case: Using the "absolute" keyword, I could avoid a typecast in this callback function:

function GetAllWindowsCallBack(Window: HWnd; List: LParam): Bool stdcall;
  Result := True;
    EnumWindows(@GetAllWindowsCallBack, Integer(List))


function GetAllWindowsCallBack(Window: HWnd; Param: LParam): Bool stdcall;
  List: TIntegerList absolute Param;
  Result := True;
LVL 11

Expert Comment

ID: 11905133
A variant record is as big as the biggest member so trashing memory should be impossible.
LVL 12

Expert Comment

ID: 11909800
never used it but I might occasionally :)
for abstraction and isolation I'm into interfaces .. leaning as much as I can to OOP
will just follow the discussion .. it's interesting enough :)
LVL 17

Author Comment

by:Wim ten Brink
ID: 11914637
Robert is correct here, though. If you use:

  B: Byte = 0;
  BB: Byte = 0;
  I: Int64 absolute B;

Then BB will be 255, not the 0 that it was initialized to. In fact, this does make the absolute keyword a bit risky if you change the datatype. Still, my point is that it's useful as an alias thus with continuously maintaining the same datatype. Still Robert has a good argment for NOT using it. :)
[We could also start a discussion about the goto statement too since quite a few people have banned this too, while others do consider it useful if properly used. But I fear that might lead to online battles. ;-) ]
LVL 17

Author Comment

by:Wim ten Brink
ID: 11972027
Well, I guess the discussion ended here. The question was originally 50 points but I'll increase it.

50 points for Robert, since he gave good reasons not to use it.

Another 30 points for Gary for his further explanation of the keyword, although I was familiar with this usage too. Still, useful for the EE archive. Your idea of using absolute in local variables is interesting but won't work. Besides, a simple typecast works just as well. :)

Expert Comment

ID: 11972185
I'm not sure what you mean by "using absolute in local variables ... won't work".  It compiles and runs without error.
LVL 17

Author Comment

by:Wim ten Brink
ID: 11990189
Geez. I never expected that it would run in local variables. Then again, in the old Turbo Pascal, you couldn't use absolute that way, if I'm not mistaken... :-)

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question