enumeration size in bitpacked records lazarus / 62-General ---------------------------------------------------------------------- 440bx · 2021-10-23 04:27 Hello, I am getting a different size than expected in bitpacked records that contain enumerated types.  Consider the following sample program: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$APPTYPE CONSOLE}  {$ifdef FPC}  {$MODESWITCH NESTEDCOMMENTS OFF}{$endif}  {$MINENUMSIZE     4} {$TYPEDADDRESS   ON} {$LONGSTRINGS  OFF}  program _BitpEnumeration; type   _1bit     = 0 ..        $1;   _30bits    = 0 .. $3FFFFFFF;  _31bits    = 0 .. $7FFFFFFF;  type  { _THREAD_UMS_INFORMATION_COMMAND                                         }   PTHREAD_UMS_INFORMATION_COMMAND = ^TTHREAD_UMS_INFORMATION_COMMAND;  TTHREAD_UMS_INFORMATION_COMMAND =  (   {   0    0 } UmsInformationCommandInvalid,   {   1    1 } UmsInformationCommandAttach,   {   2    2 } UmsInformationCommandDetach,   {   3    3 } UmsInformationCommandQuery  );  type  PTHREAD_UMS_INFORMATION = ^TTHREAD_UMS_INFORMATION;  TTHREAD_UMS_INFORMATION = {$ifdef FPC} bitpacked {$endif} record    Command                   : TTHREAD_UMS_INFORMATION_COMMAND;    { 4 }    CompletionList            : pointer;                            { 4 }    UmsContext              : pointer;                            { 4 }     case integer of    1 : (           Flags                : DWORD;                          { 4 }          );       {$ifdef FPC}      2 : (           IsUmsSchedulerThread :  _1bit;           IsUmsWorkerThread    :  _1bit;           SpareBits            : _30bits;          );      {$endif}  end;  type  PTHREAD_UMS_INFORMATION2 = ^TTHREAD_UMS_INFORMATION2;  TTHREAD_UMS_INFORMATION2 = record    Command                   : TTHREAD_UMS_INFORMATION_COMMAND;    { 4 }    CompletionList            : pointer;                            { 4 }    UmsContext                : pointer;                            { 4 }     case integer of      1 : (           Flags              : DWORD;                              { 4 }          );       {$ifdef FPC}      2 : (           BitFlags           : bitpacked record           IsUmsSchedulerThread :  _1bit;             IsUmsWorkerThread    :  _1bit;             SpareBits            : _30bits;           end;        );      {$endif}  end;  begin  writeln;  writeln;    writeln(' sizeof TTHREAD_UMS_INFORMATION  : ', sizeof(TTHREAD_UMS_INFORMATION));  writeln(' sizeof TTHREAD_UMS_INFORMATION2 : ', sizeof(TTHREAD_UMS_INFORMATION2));   writeln;  writeln;  writeln('press ENTER/RETURN to end this program');  readln;end.The output size of the first structure is 13 instead of the expected size of 16.  Changing the structure definition to exclude the enumeration from the bitpacked scope results in the expected size but this requires naming the group of bits, doing that makes it different than the original C definition. Is there a way to have the "Command" enumerated field occupy 4 bytes without having to change the scope of the bitpacked modifier ? (hopefully leaving it defined as an enumeration and not have to resort to define it as a DWORD) It seems to me that the incorrect size of the first structure is a bug but, since I am using an older version of the compiler and, I don't know if it happens in the current version, I haven't reported it. Thank you for your help. ---------------------------------------------------------------------- ASerge · 2021-10-24 19:23 Quote from: 440bx on October 23, 2021, 06:27:24 amIs there a way to have the "Command" enumerated field occupy 4 bytes without having to change the scope of the bitpacked modifier ? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TTHREAD_UMS_INFORMATION3 = bitpacked record  case Byte of    1:      (        DontUseThisFieldToAccessDataInRecordItIsOnlyByteFiller: DWORD;        CompletionList: Pointer;        UmsContext: Pointer;      case Byte of          1: (Flags: DWORD);          2:            (            IsUmsSchedulerThread: _1bit;              IsUmsWorkerThread: _1bit;              SpareBits: _30bits            );      );    2: (Command: TTHREAD_UMS_INFORMATION_COMMAND);  end; ---------------------------------------------------------------------- 440bx · 2021-10-24 20:13 Thank you Serge.  That's a good solution. @Jamie, I still think this is a bug in the compiler because the compiler is being explicitly told to use 4 bytes (DWORD) _minimum_ for the enumeration.  Enumerated types are not supposed to be bitpacked and I haven't seen the documentation stating anywhere they are susceptible to bitpacking. That said, thank you for your input.