[fpc-pascal] Implementation of variant records

Bernd Oppolzer bernd.oppolzer at t-online.de
Fri Jul 5 11:49:07 CEST 2019


This question is not directly related to FPC, but it is instead a Pascal 
question in general.

Take this record definition:

type S1 = record
             X : CHAR ( 27 ) ;
             case INTEGER of
               1 :
                 ( V : INTEGER ) ;
               2 :
                 ( W : REAL ) ;
               3 :
                 ( Z : CHAR ( 8 ) ) ;
           end ;

CHAR (n) is an abbreviation for /* packed */ array [1..n] of char
which is accepted by a certain compiler.

Now the question:

X needs 27 bytes, so the variant part of the record starts at offset 27.
But on a certain platform V has alignment 4 (integer), W has alignment 8 
(real).
The compiler decided to put V on offset 28, W on offset 32 and Z (no 
alignment)
on offset 27, so the three variants don't overlay properly.

IMO, the variants in a variant record should always overlay correctly 
(like unions in C),
so the variant part should start at offset 32 in this case, and this is 
where all three
variants should start.

In other words: every record (in classical Pascal) consists of a fixed 
part and a variant
part and a fixed border between the two (that's how it should be, IMO). 
And this border
takes its alignment properties from the highest alignment of the fields 
in the variant part.

What do you think?

Why is this important to me? Because I am working on a WITH extension 
for record definitions,
which allows the integration of the components from other record and 
pointer types into the
current record type namespace, and this has to be fixed, before I can do 
this.

BTW: is there a forum to discuss general Pascal questions not directly 
related to the FPC product?
FPC-Pascal will sure be the largest auditorium available for such 
questions ...

Thank you, kind regards

Bernd



More information about the fpc-pascal mailing list