[fpc-pascal] Array clearing

J├╝rgen Hestermann juergen.hestermann at gmx.de
Tue Apr 4 14:07:42 CEST 2017

Am 2017-04-04 um 12:51 schrieb Mark Morgan Lloyd:
 > On 02/04/17 10:00, Jonas Maebe wrote:
 >> Allocating new memory via setlength also clears the memory (+ the
 >> overhead of allocating the memory).
 > Jonas, is it still the case that if SetLength() results in existing data being moved that the original- which might be e.g. an unencrypted password- isn't cleared?

Only formerly not existing elements are overwritten via fillchar.
Already existing data is not changed of course (otherwise you would
loose them).
If you have a (dynamic) array of 5 elements and you extend it
to 7 elements via Setlength(MyArray,7) then only the last 2 (new) elements
are cleared (because otherwise they would contain garbage).

This is (and must be) independend from moving data or just extending the existing array.
If the existing elements are moved (because the allocated memory cannot be extended)
then they are not cleared (but moved).
Only formerly not existing elements are filled with zeros and
they are never moved because they did not exist before the SetLength command.

More information about the fpc-pascal mailing list