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

Sven Barth pascaldragon at googlemail.com
Sun Feb 9 17:31:42 CET 2014


On 09.02.2014 15:46, Jürgen Hestermann wrote:
> 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.

Or:

for arrayelem in MyArray do
   arrayelem.Free;

>  > 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.

Threads themselves are normal class instances. Normal class instances 
need to be freed manually (we don't have ARC for class instances yet). 
Threads however implement a "FreeOnTerminate" property that tells the 
thread to free itself once it terminates. In this case you must not free 
the thread yourself (and at best don't touch the class instance variable 
anymore after you used "Start", because the thread might already be 
terminated by that point and thus the instance freed).

Regards,
Sven



More information about the fpc-pascal mailing list