Recent

Author Topic: size of record  (Read 1120 times)

JdeHaan

  • Full Member
  • ***
  • Posts: 121
size of record
« on: November 20, 2021, 02:12:01 pm »
Hi,

I have this record defined:

Code: Pascal  [Select][+][-]
  1.   TBitData = packed record
  2.     case Byte of
  3.       0: (Bits64: UInt64);
  4.       1: (Bits32: array[0..1] of UInt32);
  5.       2: (Num: Double);
  6.   end;
  7.  

My question is, what is the exact size? Is it 64 bits or do I have to add 1 byte for the 'case Byte of'.
If the latter, how can I get a record with exactly 64 bits?


BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: size of record
« Reply #1 on: November 20, 2021, 02:25:13 pm »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. type
  4.   TBitData = packed record
  5.     case byte of
  6.       0: (Bits64: uint64);
  7.       1: (Bits32: array[0..1] of uint32);
  8.       2: (Num: double);
  9.   end;
  10. begin
  11.   WriteLn(SizeOf(TBitData));
  12.   ReadLn;
  13. end.            
8

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11455
  • FPC developer.
Re: size of record
« Reply #2 on: November 20, 2021, 02:25:33 pm »
64-bit. The extra byte for the selector comes when you name the switch variable instead of just using the type, like:
Code: Pascal  [Select][+][-]
  1.       TBitData = packed record
  2.         case selector : Byte of
  3.           0: (Bits64: UInt64);
  4.           1: (Bits32: array[0..1] of UInt32);
  5.           2: (Num: Double);
  6.       end;
  7.  
     
« Last Edit: November 20, 2021, 03:33:14 pm by marcov »

JdeHaan

  • Full Member
  • ***
  • Posts: 121
Re: size of record
« Reply #3 on: November 20, 2021, 03:09:23 pm »
Thanks


 

TinyPortal © 2005-2018