[fpc-devel] TBytes

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


Am 28.02.2012 05:04, schrieb Martin Schreiber:
> Hi,
> I read that we should use TBytes instead of AnsiString in order to
> implement combined binary/character buffers with automatic memory
> management. With AnsiString we used setlength() in order to allocate not
> initialized memory.
> TBytes is defined as
> "
> TBytes = array of Byte;
> "
> where setlength() fills the buffer with zeros. What should we use instead?

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.

Regards,
Sven



More information about the fpc-devel mailing list