Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

Author Topic: size of variant records  (Read 2560 times)

440bx

• Hero Member
• Posts: 2848
size of variant records
« on: January 11, 2022, 12:54:00 am »
Hello,

Does FPC provide a way to determine the size of a variant record up to and including a specific field/variant ?

For instance, in the following sample:
Code: Pascal  [Select][+][-]
1. {\$APPTYPE CONSOLE}
2.
3. type
4.   TSOME_VARIANT_TYPE = record
5.     VariantSize          : DWORD;
6.
7.     SomeField            : qword;
8.
9.     case integer of
10.       1 : (
11.            a : byte;
12.           );
13.
14.       2 : (
15.            b : word;
16.           );
17.
18.       3 : (
19.            c : DWORD;
20.           );
21.
22.       4 : (
23.            d : qword;
24.           );
25.   end;
26.
27. var
28.   Size : DWORD;
29.
30. begin
31.
32.   { how to get the size up to and including field a or b or c ?               }
33.
34.   { Size := ???                                                               }
35. end.
Is there a way to determine the size of the record up to and including a (but not b, c and d) ? or upto and including b (or c) excluding the fields that follow it ?

FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

engkin

• Hero Member
• Posts: 3112
Re: size of variant records
« Reply #1 on: January 11, 2022, 02:16:40 am »
Add the offset of the field to the SizeOf the field?

440bx

• Hero Member
• Posts: 2848
Re: size of variant records
« Reply #2 on: January 11, 2022, 03:31:55 am »
Add the offset of the field to the SizeOf the field?
Yes, that will do it.  I was hoping for something like "SizeTo(<Record.Fieldname>);  Can't have it all, I guess

Thank you for the suggestion.  At least it tells me that there isn't a really straightforward/direct way of getting the size up to some field.

FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

engkin

• Hero Member
• Posts: 3112
Re: size of variant records
« Reply #3 on: January 11, 2022, 04:21:57 am »
On a positive note, it is a compile-time value.

PascalDragon

• Hero Member
• Posts: 4168
• Compiler Developer
Re: size of variant records
« Reply #4 on: January 11, 2022, 09:28:28 am »
Add the offset of the field to the SizeOf the field?
Yes, that will do it.  I was hoping for something like "SizeTo(<Record.Fieldname>);  Can't have it all, I guess

Thank you for the suggestion.  At least it tells me that there isn't a really straightforward/direct way of getting the size up to some field.

Well, the size up to some field is the offset of that field

440bx

• Hero Member
• Posts: 2848
Re: size of variant records
« Reply #5 on: January 11, 2022, 09:34:45 am »
Well, the size up to some field is the offset of that field
Wouldn't it be the offset to the _next_ field ? (which makes the expression to calculate the size a bit confusing)
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

PascalDragon

• Hero Member
• Posts: 4168
• Compiler Developer
Re: size of variant records
« Reply #6 on: January 11, 2022, 09:52:19 am »
Well, the size up to some field is the offset of that field
Wouldn't it be the offset to the _next_ field ? (which makes the expression to calculate the size a bit confusing)

Right, forgot that you meant including the field