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

Jürgen Hestermann juergen.hestermann at gmx.de
Sun Feb 9 15:46:57 CET 2014


Am 2014-02-09 15:10, schrieb Fred van Stappen:
 >  > if length(MyArray) > 0 then
 >  > for x := 0 to high(MyArray) do
 >  > MyArray[x].Free;

As I have learned just recently ;-) this code could be shortened by

for x := low(MyArray) to high(MyArray) do
    MyArray[x].Free;

if x is a signed integer. So you would save the length check.


 > Because the threads are already freed (yes/no) ?

Yes, that's tricky. *Some* (managed) data structures like ansistrings and dynamic arrays are freed by the compiler. But if you requested memory by yourself (new, getmem) then you need to clean it up yourself too. I am not sure what applies to threads but I would think that they are managed by the compiler so that a setlength(MyArray,0) would automatically free all (automatically) allocated data.




More information about the fpc-pascal mailing list