Forum > Windows

Maintaining Alignment of records across units.


Maintaining Alignment of records across units.

I am moving a large medical notes and billing system from Delphi 7 to Lazarus in Windows but the plan is to run under Linux.

It seems that I cannot maintain consistent record sizes across units in a single project Lazarus/FPC.

Before proving a code example, I will try and explain what is happening.

The 'packed' records contain fields of known length, of strings (String[21]) and integers (4 bytes) as well as some enumerated types (one byte) that have been used for years in a number of networked installations. The records are written to and read from typed files of the same record type. In Lazarus/FPC, I have used {$A1} and {$MINENUMSIZE 1} to force field alignment and enumerated types into the smallest memory to match the storage allocation by Delphi.

The Sizeof() intrinsic reports the correct (same as Delphi) record size if used in the unit where the record is declared. According the records are read and written to the data files correctly from that unit.

BUT from any other unit, the sizeof() intrinsic reports a larger value of types and records declared in the original 'Types' unit interface.

So, procedures written in a unit that USES the original 'Types' unit that declared the records, results in incorrect access the data files.

I have types declared in one 'Types' unit and file access in another unit.

I have a main unit that defines record types like this
Unit Types;
  {$MODE Delphi}
{$A-}  //for Delphi32 so that the structures are the same size as the DOS and windows versions
  {$A1}   //pack on single byte boundaries for FPC/Lazarus
{$MINENUMSIZE 1}  //So that enumerated types are same size as in Delphi
  Transaction = Packed record

And another unit that implements file access with the same complier directives as listed above.

QUESTION: As this works in Delphi, am I missing a project wide setting in the Lazarus IDE to maintain minimum record size across units?  I have been through all Project options but cannot find such a setting. Is there something else ? $CODEALIGN.  ??

Any help greatly appreciated.
Lazarus 1.4.2  FPC 2.6.2  Windows 7

FPC knows the bitpacked specifier, so records can be declared that are guaranteed to always occupy exactly the anticipated size.

--- Code: ---type
  myExactSizeRec = bitpacked record
    field1: word;
    field2: byte;
    .. etc.

--- End code ---

(This also allows you to declare records whose size is not an integral multiple of byte).

Actually, that should be impossible unless there's a mix of old & new .ppu or identifier shadowing. Prove that it really happens with the attached project (it shows 27 for all 3 calls here).

LeLedumbo you are right there was a mixture of old and new.

Thanks howardpc for the bitpacked directive, I was not aware of that.

It was a programming error and I've spent 2 days tracking it down. I'm embarrassed...there are many units involved and after checking all record fields, I discovered that a string field that I assumed was String[21] was in fact re-declared as  String[100] in a substitution include file before the record declaration.


[0] Message Index

Go to full version