[fpc-pascal] Array clearing

Sven Barth pascaldragon at googlemail.com
Sat Apr 1 09:39:11 CEST 2017

Am 01.04.2017 05:42 schrieb "Ryan Joseph" <ryan at thealchemistguild.com>:
> As far as the compiler is concerned what’s the difference between
clearing an array using a for-loop vs. FillChar? It seems like iterating
the array would be slower but what does FillChar do exactly and is it
faster? The primary concern here is that the memory originally allocated
(using SetLength right?) remains in the same location.
> var
>   list: array of integer;
> SetLength(list, 10);
> for i := 0 to high(list) do
>   list[i] := 0;
> FillChar(list[0], Length(list) * sizeof(integer), 0);

It totally depends on the type. In case of primitive types like integers
there is indeed only the performance difference (though if you know that
the element size is four FillDWord could be even faster, depending on the
implementation in the RTL).
If you have managed types however or class instances the result of the
FillChar (or equivalent) would be memory leaks.

Plase also note that after a SetLength the new elements are already 0 (or

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170401/969ff549/attachment.html>

More information about the fpc-pascal mailing list