Forum > Beginners
[Solved] Compiler allows un-indexed Array as Absolute argument- is this desired?
Ten_Mile_Hike:
The code below is PURPOSELY INCORRECT.
The compiler allows it and interprets D to always be = to A[1]
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender:TObject);var A:Array[1..4] of Integer=(13,10,11,12); D:Integer Absolute A; {Something like A[3] would be correct here, NOT A}begin Button1.Caption := inttostr(D); //Returns A[1] or "13" as the Button captionend;
My question is should the compiler be accepting "Absolute A" instead of the correct A[index] or
should this produce an "Error" instead
jamie:
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.
Martin_fr:
"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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
--- Quote from: Ten_Mile_Hike on July 18, 2024, 12:15:37 am ---My question is should the compiler be accepting "Absolute A" instead of the correct A[index] or
should this produce an "Error" instead
--- End quote ---
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.
Khrys:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function Foo(): Integer;var Bar: Integer absolute $DEADBEEF;begin // Compiles with no warnings Result := Bar; // Equivalent to Result := PInteger(Pointer($DEADBEEF))^end;
Navigation
[0] Message Index
[#] Next page