[fpc-pascal] Array clearing
Sven Barth
pascaldragon at googlemail.com
Sun Apr 2 18:02:03 CEST 2017
On 02.04.2017 11:22, Ryan Joseph wrote:
>
>> On Apr 1, 2017, at 9:25 PM, Jürgen Hestermann <juergen.hestermann at gmx.de> wrote:
>>
>> If you just need to reuse the same array and only need to zero its elements
>> then of course fillchar would be the fastest approach (it saves the memory
>> reallocation step).
>
> Why is this “of course”? What’s the implementation of FillChar exactly? For all I know it has to iterate over a range of bytes and this could be slower than allocation of memory. Maybe some of the compiler people know these details.
The question is not about the implementation of FillChar. It's about
reallocating a dynamic array as to reallocate it you'd need to do
=== code begin ===
SetLength(arr, 0);
SetLength(arr, newlen);
=== code end ===
And that this is more complex than
=== code begin ===
FillChar(arr, SizeOf(arr[0]) * Length(arr), 0);
=== code end ===
can be easily seen by looking at the implementation of SetLength() in
$fpcdir/rtl/inc/dynarr.inc, fpc_dynarray_setlength().
Especially since line 220 (at least in the current revision) contains a
call to FillChar(), the whole ordeal can only be *more* complex than
FillChar().
Regards,
Sven
More information about the fpc-pascal
mailing list