[fpc-devel] TBytes

Martin Schreiber mse00000 at gmail.com
Tue Feb 28 08:40:11 CET 2012


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

Martin



More information about the fpc-devel mailing list