[fpc-pascal] BlockWrite of dynamic arrays

Bo Berglund bo.berglund at gmail.com
Sun Jul 30 08:20:42 CEST 2017

I am converting older code to move from AnsiString to TBytes as
non-text data container. While doing it I am using conditionals so I
don't destroy the working code until I have verified that the TBytes
usage is OK.

Now I have encountered use of BlockWrite where I would like to do the

1) Change variable definitions from

F: File of AnsiChar;
Buffer: AnsiString;

F: File;
Buffer: TBytes;

2) Specifically set the record length to 1 using
Reset(F, 1);

3) Change the BlockWrite command from
BlockWrite(F, Buffer[1], Length(Buffer));
BlockWrite(F, Buffer, Length(Buffer));

Note that I would like to *not* specify the index of the first element
of the array so that the same code can work for both cases depending
on conditional setting.
But I am worried that Buffer will reference a different memory
location than Buffer[0].

So are these two calls equivalent when Buffer is an AnsiString or a
TBytes array:
BlockWrite(F, Buffer[0], Length(Buffer));
BlockWrite(F, Buffer, Length(Buffer));

Bo Berglund
Developer in Sweden

More information about the fpc-pascal mailing list