[fpc-devel] Feature request/discussion - SetLengthNoInit

Sven Barth pascaldragon at googlemail.com
Wed Sep 16 17:17:25 CEST 2020


Benito van der Zander via fpc-devel <fpc-devel at lists.freepascal.org>
schrieb am Mi., 16. Sep. 2020, 15:30:

>
> Hi,
>
>
> I'm against adding support for this. Dynamic arrays have guaranteed
> behavior. If users find that this behavior is not appropriate for their use
> case then they should not use them (or as you already suggested,
> preallocate them).
>
>
> well, setlength does not always initialize the array
> That is why the new fpc versions always show a "might be
> uninitialized"-warning, when setlength is called without setting the array
> to nil first
>

Then you have not understood what the warning is for. If you cal SetLength
FPC will always initialize the new memory area that is added. However if
you use SetLength on a Result variable then this array might already be
initialized. So if the Result variable should have 5 elements upon function
entry and you set the length of the array to 2 then the first two entries
will retain their value. If you set it to 7 instead then the first 5
entries will be kept and the new two will be initialized.
The RTL behaves here as described: it always initializes new parts of the
array.

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20200916/13927dc3/attachment.htm>


More information about the fpc-devel mailing list