How do I recreate a function that retrieves the tallest element of an object?

My plan is to make a function that fetches the tallest element of the object, so to speak, the top range of the array. In other words, I am trying to get the High () function code.

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 to 1. However, it does not return correct results, either as static or dynamic array type.

How do I recreate the High () function in Pascal?

+2


source to share


2 answers


Not sure why you want to do this when Delphi already has a built-in compiler magic High () function, but here it goes.

Static arrays: not possible. The size information is not saved at runtime because the size is known to the compiler and cannot be changed. High () simply returns the required number to 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 discards 4 bytes from the beginning of the array (you only indent 1) and returns the length as an integer, not a byte.

Hope this is helpful. Why aren't you using High, BTW?

+4


source


The High () (and Low ()) functions are called "standard functions", which means that they are an integral part of the compiler. Like Write and Writeln, they don't really exist as regular Pascal function declarations. The compiler makes sure they are in the System area just as a convenience and allows the same name to be used in other areas. It also allows you to qualify them with System.High () to explicitly refer to the system unit version. Since they are internal, the compiler will automatically generate the correct code sequence for the type in question. This also means that trying to duplicate their full functionality is nearly impossible. Just stick to the built-in standard functions.



+5


source







All Articles