[fpc-pascal] Does SetLength clear its elements to zeros?

Dennis Poon dennis at avidsoft.com.hk
Fri Dec 11 12:38:18 CET 2015



Jonas Maebe wrote:
>
> Dennis wrote on Fri, 11 Dec 2015:
>
>> I just tested, SetLength a string  does not zero its elements (the 
>> chars).
>>
>> However, SetLength a dynamic array seems to zero its elements.
>
> Dynamic arrays are indeed zeroed when changing the length, while 
> strings are not. For dynamic arrays, it is necessary anyway in case it 
> has reference counted elements. Maybe for consistency, it's always done.
>
> Additionally, changing the length of the string without afterwards 
> overwriting the data between the previous and new last character seems 
> something that would happen very seldom (why did you increase the 
> length if you didn't need to write all characters -- any operation 
> with the string, such as writing or concatenating it, will use those 
> characters anyway because the length indicates they're part of the 
> string).
>
> The above are mostly guesses though.
>
>
> Jonas
> _______________________________________________
>
Thanks for clarifications.

Dennis




More information about the fpc-pascal mailing list