We have the following pragma and the bits are not properly packed in this 32 bits application. #pragma pack(1) is not recognized as a directive.
pragma pack(1)
typedef struct
{
unsigned long field1 : 8;
unsigned long field2 : 4;
unsigned long field3 : 1;
unsigned long field4 : 1;
unsigned long filed5 : 1;
unsigned long field6 : 1;
unsigned long field7 : 10;
unsigned long field8 : 10;
unsigned long field9 : 10;
unsigned long field10 : 10;
unsigned long field11 : 2;
unsigned long field12 : 2;
unsigned long field13 : 2;
unsigned long field14 : 2;
}t_bitfield_s;
typedef union
{
t_bitfield_s Struct;
unsigned char Array[8];
}t_bitfield_u;
pragma pack()
t_bitfield_u Test;
Data is not packed as expected. field1 up to field7 sum in total 26 bits. In the same unsigned long (32 bits) storage it would be possible to pack 6 additional bits from field 8. Due to the fact that the pragma is not considered, a new storage for unsigned long is started.
Expected Behavior:
Test.Array[0] = field1 (8 bits)
Test.Array[1] = field2 (4bits) field3 (1bit) field4 (1bit) field5 (1bit) field6 (1bit)
Test.Array[2] = field7 (8 bits)
Test.Array[3] = field7 (remaining 2 bits) field 8 (6 bits)
Test.Array[4] = field 8 (remaining 4 bits) field 9 (4 bits)
Test.Array[5] = field 9 (remaining 6 bits) field 10 (2 bits)
Test.Array[6] = field 10 (remaining 8 bits)
Test.Array[7] = field 11 (2bits) field 12 (2bits) field 13 (2bits) field 14 (2bits)
Actual Behavior:
Test.Array[0] = field1 (8 bits)
Test.Array[1] = field2 (4bits) field3 (1bit) field4 (1bit) field5 (1bit) field6 (1bit)
Test.Array[2] = field7 (8 bits)
Test.Array[3] = field7 (remaining 2 bits)
Test.Array[4] = field8 (8 bits)
Test.Array[5] = field8 (remaining 2 bits) field 9 (6 bits)
Test.Array[6] = field9 (remaining 4 bits) field 10 (4 bits)
Test.Array[7] = field 10 (remaining 6 bits) field 11 (2bits)
Test.Array[8] = field 12 (2bits) field 13 (2bits) field 14 (2bits)
Test.Array[9] = 0
Test.Array[10] = 0
Test.Array[11] = 0
What specifier/pragma can be used for MVS2015/2017/2019 to have the expected behaviour?
Thank you!