[fpc-devel] TBytes

Sven Barth pascaldragon at googlemail.com
Tue Feb 28 09:46:23 CET 2012


Am 28.02.2012 08:40, schrieb Martin Schreiber:
> Am 28.02.2012 09:23, schrieb Sven Barth:
>>
>> For strings SetLength also fills the string with zeros as the following
>> example shows:
>>
>> === example begin ===
>>
>> program setlengthtest;
>>
>> var
>> barr: array of Byte;
>> b: Byte;
>> s: AnsiString;
>> c: Char;
>> begin
>> SetLength(barr, 20);
>> for b in barr do
>> Write(b, ' ');
>> Writeln;
>> SetLength(s, 20);
>> for c in s do
>> Write(Ord(c), ' ');
>> Writeln;
>> end.
>>
>> === example end ===
>>
>> === output begin ===
>>
>> PS P:\tests\oneshots> .\setlengthtest.exe
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>>
>> === output end ===
>>
>> So SetLength is the operation to use in each case.
>>
> This is probably because in your testcase the memory is still clean, try
> with -gh. Additionally this behavior would not be Delphi compatible:
> "
> When you use SetLength, existing characters in the string are preserved,
> but the contents of any newly allocated space is undefined.
> "
> http://docwiki.embarcadero.com/RADStudio/en/Declaring_and_Initializing_Strings

You are right, I checked the code of SetLength for arrays and strings 
and only arrays use FillChar. Nevertheless there is no other operation 
you can use for arrays.

Regards,
Sven




More information about the fpc-devel mailing list