Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-08

Variant record consistency

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mark Morgan Lloyd

Given a declaration

const
   topDataQword= 29;
   topNameChar= 231;
type
   Tfnord= record
             case boolean of
               false: (data: array[0..topDataQword] of qword);
               true:  (name: array[0..topNameChar] of char;
                       inode: qword)     (* For recovery if name is
changed      *)
           end;

is it possible to enforce a rule that the two record variants must be the same size?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Gerhard Scholz
to Mark Morgan Lloyd: Given a declaration const topDataQword= 29; topNameChar= 231; type Tfnord= record case…

if same size is necessary, I would do it like this:

const
  topDataQword= 29;
  topNameChar= 231;
type
  Tfnord1 = array[0..topDataQword] of qword ;
  Tfnord2 = record
                       name: array[0..topNameChar] of char;
                       inode: qword
                     end ;
  Tfnord= record
             case boolean of
               false: (data: Tfnord1);
               true:  (ni : Tfnord2)
           end;
and, in the code:
  if sizeof(tfnord1)  sizeof(tfnord2) then halt(4711);

or, in the data
type xxxxxxtest = array [ 1..ord(sizeof(tfnord1) = sizeof(tfnord2)) ] of boolean ;

Earlier message 34 lines
----- Original Message ----- From: "Mark Morgan Lloyd" To: Sent: Sunday, August 07, 2016 6:11 PM Subject: [fpc-pascal] Variant record consistency > Given a declaration > > const > topDataQword= 29; > topNameChar= 231; > > type > Tfnord= record > case boolean of > false: (data: array[0..topDataQword] of qword); > true: (name: array[0..topNameChar] of char; > inode: qword) (* For recovery if name is changed > *) > end; > > is it possible to enforce a rule that the two record variants must be the > same size? > > -- > Mark Morgan Lloyd > markMLl .AT. telemetry.co .DOT. uk > > [Opinions above are the author's, not those of his employers or > colleagues] > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Tomas Hajny
to Mark Morgan Lloyd: Given a declaration const topDataQword= 29; topNameChar= 231; type Tfnord= record case…

On Sun, August 7, 2016 18:11, Mark Morgan Lloyd wrote:

Hello Mark,

Show quoted text 17 lines
Given a declaration const topDataQword= 29; topNameChar= 231; type Tfnord= record case boolean of false: (data: array[0..topDataQword] of qword); true: (name: array[0..topNameChar] of char; inode: qword) (* For recovery if name is changed *) end; is it possible to enforce a rule that the two record variants must be the same size?

What would the rule supposedly do? As far as I know, all the variants take the size of the largest one technically (in memory). Do you mean that the compiler should warn if their declared size differs?

Tomas

Gerhard Scholz
to Gerhard Scholz: if same size is necessary, I would do it like this: const topDataQword= 29; topNameChar=…

I assume, my response was a bit unclear. If you code it like this, you should get a runtime-error or compiler-error if the two variants are not same-sized, and I assume, this is what you wanted.

Earlier message 70 lines
----- Original Message ----- From: "Gerhard Scholz" To: "FPC-Pascal users discussions" Sent: Sunday, August 07, 2016 8:00 PM Subject: Re: [fpc-pascal] Variant record consistency > if same size is necessary, I would do it like this: > > const > topDataQword= 29; > topNameChar= 231; > > type > Tfnord1 = array[0..topDataQword] of qword ; > Tfnord2 = record > name: array[0..topNameChar] of char; > inode: qword > end ; > Tfnord= record > case boolean of > false: (data: Tfnord1); > true: (ni : Tfnord2) > end; > > and, in the code: > if sizeof(tfnord1) sizeof(tfnord2) then halt(4711); > > or, in the data > type xxxxxxtest = array [ 1..ord(sizeof(tfnord1) = sizeof(tfnord2)) ] of > boolean ; > > ----- Original Message ----- > From: "Mark Morgan Lloyd" > To: > Sent: Sunday, August 07, 2016 6:11 PM > Subject: [fpc-pascal] Variant record consistency > > >> Given a declaration >> >> const >> topDataQword= 29; >> topNameChar= 231; >> >> type >> Tfnord= record >> case boolean of >> false: (data: array[0..topDataQword] of qword); >> true: (name: array[0..topNameChar] of char; >> inode: qword) (* For recovery if name is >> changed *) >> end; >> >> is it possible to enforce a rule that the two record variants must be the >> same size? >> >> -- >> Mark Morgan Lloyd >> markMLl .AT. telemetry.co .DOT. uk >> >> [Opinions above are the author's, not those of his employers or >> colleagues] >> _______________________________________________ >> fpc-pascal maillist - >> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Mark Morgan Lloyd
to Tomas Hajny: What would the rule supposedly do? As far as I know, all the variants take the size of…
Tomas Hajny wrote 8 lines
On Sun, August 7, 2016 18:11, Mark Morgan Lloyd wrote: > is it possible to enforce a rule that the two record variants must be > the same size? What would the rule supposedly do? As far as I know, all the variants take the size of the largest one technically (in memory). Do you mean that the compiler should warn if their declared size differs?

Noting obviously Jonas's and Gerhard's comments.

I was wondering whether the compiler could warn if it found that it had to insert padding since one or other of the variants didn't reach the end of the overall record, the size of which is obviously determined by the largest variant. Obviously, as Gerhard points out, there's ways round it, and I've already got a number of mandatory size checks (i.e. not just assertions) to catch things that smell unusual.

Area of application in this case is laying out a file structure to support a mainframe emulator, where the guest filesystem has no consistency checks so as much as possible needs to be done by the host. However similar requirements also apply wherever a data structure needs to overlay an existing file format or structure in memory.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]