[fpc-pascal] High() and Low() for empty dynamic arrays

Jürgen Hestermann juergen.hestermann at gmx.de
Sun Feb 9 10:11:14 CET 2014


Am 2014-02-08 19:53, schrieb Florian Klämpfl:
 > You didn't have only a question but you made also a proposal for a
 > language change. And when proposing a change it is very usefull to have
 > an example which demonstrates the reason for the change.

What code should I have written?
I was convinced that Low() and High() both give back zero.
I did not ask how High/Low work.
I thought I had understood it.
So what code should I write?


My missinterpretation was based on two facts:

1.) I had switched off range checking
2.) I used a cardinal variable in my for loop

That lead to the circumstance that my for loop was entered
even when the dynamic array was empty.
And then I got an exception when accessing the element with index 0:

-------------------------------------
var i : Cardinal;
for i := low(MyArray) to High(MyArray) do
    MyArray[i] :=    <--- the debugger put me here with an exception
-------------------------------------

So I thought that High and Low both give back zero.
How otherwise could I end up within the for loop when the array is empty?

Actually the error already occured in the line before:
High() gave back -1 but was assigned to a cardinal variable.
With range check on I would have got an error here already.
But with range check off it was simply assigned so that
the high value now was a very high positive value!
Therefore the for loop was entered although it should not do so.

Under this impression, what kind of code should I have written?




More information about the fpc-pascal mailing list