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
Solved

[Discussion] The absolute keyword...

Posted on 2004-08-26
10
761 Views
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. ;-)
0
Comment
Question by:Wim ten Brink
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 2

Assisted Solution

by:gary_williams
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.
0
 
LVL 11

Accepted Solution

by:
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.
0
 
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.
0
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.

 
LVL 2

Expert Comment

by:gary_williams
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;
begin
  TIntegerList(List).Add(Window);
  Result := True;
end;
...
    EnumWindows(@GetAllWindowsCallBack, Integer(List))


becomes

function GetAllWindowsCallBack(Window: HWnd; Param: LParam): Bool stdcall;
var
  List: TIntegerList absolute Param;
begin
  List.Add(Window);
  Result := True;
end;
0
 
LVL 11

Expert Comment

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

Expert Comment

by:Lee_Nover
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 :)
0
 
LVL 17

Author Comment

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

var
  B: Byte = 0;
  BB: Byte = 0;
  I: Int64 absolute B;
begin
  I := $FFFFFFFFFFFFFFFF;
  WriteLn(BB);
end.

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. ;-) ]
0
 
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. :)
0
 
LVL 2

Expert Comment

by:gary_williams
ID: 11972185
I'm not sure what you mean by "using absolute in local variables ... won't work".  It compiles and runs without error.
0
 
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... :-)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

789 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