I got stuck about #pragma pack(1)
wrong behavior when define a 6-bit
field and assumes it as 8-bit
. I read this question to solving my problem but it doesn't help me at all.
In Visual Studio 2012 I defined bellow struct
for saving Base64
characters :
#pragma pack(1)
struct BASE64 {
CHAR cChar1 : 6;
CHAR cChar2 : 6;
CHAR cChar3 : 6;
CHAR cChar4 : 6;
};
Now I got its size with sizeof
, but the result isn't what I expected :
printf("%d", sizeof(BASE64)); // should print 3
Result : 4
I was expect that get 3
(because 6 * 4 = 24
, so 24
bit is 3
byte)
Event I tested it with 1-bit
field instead and got correct size (1-byte) :
#pragma pack(1)
struct BASE64 {
CHAR cChar1 : 2;
CHAR cChar2 : 2;
CHAR cChar3 : 2;
CHAR cChar4 : 2;
};
Actually, why 6-bit
assumes 8-bit
with #pragma pack(1)
?