[fpc-pascal] BlockWrite of dynamic arrays

Sven Barth pascaldragon at googlemail.com
Sun Jul 30 09:38:49 CEST 2017


Am 30.07.2017 08:21 schrieb "Bo Berglund" <bo.berglund at gmail.com>:
> 3) Change the BlockWrite command from
> BlockWrite(F, Buffer[1], Length(Buffer));
> to
> 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));

Like with String you need to reference the first element with dynamic
arrays, in this case Buffer[0], otherwise you'll write the management
record and following unrelated data.
You can either use "Low(buffer)" as the index which *should* work with
strings as well, though I don't know whether it always did... If that
doesn't work simply write two overloaded inline functions that return the
index of the first element depending on whether it's String or TBytes.

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170730/f23abc53/attachment.html>


More information about the fpc-pascal mailing list