My plan is to make a function that retrieves the highest element of an object, so to speak, the upper range of an array.In other words,I’m trying to get the the code of the function High().
What I have tried so far:
function High2(var X):integer;
begin
Result:=Pbyte(Cardinal(@X)-1)^-1;
end;
The function above should read the value(length) before the position of the first element in the object(array/string) and return it decreased by 1.However It doesn’t retrieve correct results neither on static nor dynamic array type.
How do I recreate the High() function in Pascal?
Not sure why you’d want to do that when Delphi already has a built-in High() compiler magic function, but OK, here goes.
Static arrays: Can’t be done. No size information is stored at runtime since the size is known to the compiler and can’t change. High() just drops the necessary number into the code as a constant.
Dynamic arrays: The compiler translates High to a call to DynArrayHigh in the System unit, which returns DynArrayLength – 1. DynArrayLength steps back 4 bytes from the start of the array (you’re only stepping back 1) and returns the length as an integer instead of a byte.
Hope this is helpful. Why aren’t you just using High, BTW?