[Lazarus] LongString does not work?

Sven Barth pascaldragon at googlemail.com
Sun Mar 3 14:34:48 CET 2013


On 03.03.2013 14:26, Xiangrong Fang wrote:
> A (Ansi)string in {$H+} mode is just a pointer (to the characters of the
> string). So sizeof(DataFolder)=4 (or 8) in all cases. There is no room
> for the string unless
>
> I guess this is the cause, but then there is no need to specify "packed"
> if I just use array of char.

The "packed" should always be used if you want to read/write data. Only 
then you can be sure that the memory layout of the record is correct. 
Otherwise padding bytes will be added. Consider the following record:

=== example begin ===

type
   TMyRecord = record
     b: Byte;
     i: LongInt;
     w: Word;
   end;

=== example end ===

The size of this record will be 12, because between b and i and after w 
padding bytes are added. While if you add "packed" the size will be 7 
and the fields will directly follow after each other.

The memory layout of non-packed records is not guaranteed to stay the 
same between FPC versions! It could be for example that we find a bug in 
the code that generates the memory layout of a record and then suddenly 
you can no longer read in files that were written by a version of your 
program compiled with the older compiler. For "packed" there is the 
guarantee though that the layout will stay the same.

Regards,
Sven




More information about the Lazarus mailing list