Recent

Author Topic: [Solved] Compiler allows un-indexed Array as Absolute argument- is this desired?  (Read 1256 times)

Ten_Mile_Hike

  • Jr. Member
  • **
  • Posts: 87
The code below is PURPOSELY INCORRECT.
The compiler allows it and interprets D to always be = to A[1]

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender:TObject);
  2. var
  3.     A:Array[1..4] of Integer=(13,10,11,12);
  4.     D:Integer Absolute A;  {Something like A[3] would be correct here, NOT A}
  5. begin
  6.     Button1.Caption := inttostr(D);  //Returns A[1] or "13" as the Button caption
  7. end;      

My question is should the compiler be accepting "Absolute A" instead of the correct A[index] or
 should this produce an "Error" instead
« Last Edit: July 21, 2024, 11:59:10 pm by Ten_Mile_Hike »
When any government, or any church for that matter, undertakes to say to its subjects, This you may not read, this you
must not see, this you are forbidden to know, the end result is tyranny and oppression no matter how holy the motives.

Robert A. Heinlein

jamie

  • Hero Member
  • *****
  • Posts: 6735
what is not correct?

Looks like it's working correctly.

Why would you think otherwise? Maybe it's a different language you come across that does that.


The only true wisdom is knowing you know nothing

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10553
  • Debugger - SynEdit - and more
    • wiki
"Absolute" is not type safe. And it isn't meant to be afaik.  In fact it is often used as a way to access on variable as something of another type.

Often time, those "type translations" are such that they could be done by typecase (maybe pointer to TObject), but that isn't necessary.

You could have a record "TPoint = record x,y: integer; end". And if you know that "x" is the first thing in memory, and it is right at the start of the record, then you could have an integer variable "absolute" at an variable of type TPoint.

In other words, it just takes the address, and gets whatever is in memory.
so it would then do the same as
Code: Pascal  [Select][+][-]
  1. PInteger(@MyPoint)^

It does the same in your case.

You have a static array, and a static array is not a pointer, it is straight there in memory, like a record would be. So the address of the array "A" and the entry "A[1]" are the same.

Hence it works.
(It wouldn't work with a dynamic array)

440bx

  • Hero Member
  • *****
  • Posts: 4738
My question is should the compiler be accepting "Absolute A" instead of the correct A[index] or
 should this produce an "Error" instead
in addition to what Jamie and Martin already said, the argument of "absolute" is a memory reference and "A" _is_ a memory reference, specifically it is where the array is located in memory, therefore it is _not_ an error.

As long as "absolute" is given a memory reference there is no problem.  You can even use the name of _typed_ constant (because a typed constant is actually a variable, thus a memory reference.)

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Khrys

  • Full Member
  • ***
  • Posts: 105
The  absolute  keyword has assembler-level powers; any type/memory safety checks go straight out the window when using it. You can even use hard-coded addresses for  absolute  variables:

Code: Pascal  [Select][+][-]
  1. function Foo(): Integer;
  2. var
  3.   Bar: Integer absolute $DEADBEEF;
  4. begin                // Compiles with no warnings
  5.   Result := Bar;     // Equivalent to Result := PInteger(Pointer($DEADBEEF))^
  6. end;

TRon

  • Hero Member
  • *****
  • Posts: 3631
"Absolute" is not type safe. And it isn't meant to be afaik.  In fact it is often used as a way to access on variable as something of another type.
It is indeed not meant to be typesafe.

If it would then it would render the use of it useless and not compatible to its original implementation.

Therefor always handle the use of the absolute modifier with care when using it.
« Last Edit: July 18, 2024, 08:31:58 am by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

jamie

  • Hero Member
  • *****
  • Posts: 6735
Real men program in Assembler, some may even use "Absolute" occasionally!
The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 4738
"absolute" is handy to access Windows USER_SHARED_DATA which is always at the same address.

"absolute" is one of FPC's best features.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

silvercoder70

  • Jr. Member
  • **
  • Posts: 93
    • Tim Coates
Or... Absolute(ly)

:)
Explore the beauty of modern Pascal programming with Delphi & Free Pascal - https://www.youtube.com/@silvercoder70

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11944
  • FPC developer.
Real men program in Assembler, some may even use "Absolute" occasionally!

Only in combination with goto's !

Thaddy

  • Hero Member
  • *****
  • Posts: 16186
  • Censorship about opinions does not belong here.
Real men do not show off their assembler "knowledge", because they know they are outperformed by good high level language compilers.
I make an exception for you, Marco.
I have lost the plot when we needed to take care of cache stalls and the lot... :)
(that is rather poëtic, lost, plot, lot  ;D)

Back to original question: this is already well explained, it is the memory access and that is intentional.
« Last Edit: July 19, 2024, 12:48:56 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018