[Discussion] The absolute keyword...

Posted on 2004-08-26
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
  • 4
  • 3
  • 2
  • +1

Assisted Solution

gary_williams earned 30 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 50 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.

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.
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now