Why this union's size is 2 with bitfields?

前端 未结 6 1280
独厮守ぢ
独厮守ぢ 2021-01-06 15:14

I am working on turbo C on windows where char takes one byte.Now my problem is with the below union.

union a
{
 unsigned char c:2;
}b;
void main()
{
printf(\         


        
6条回答
  •  旧巷少年郎
    2021-01-06 16:00

    Turbo C is based on 8086 microprocessor which has two byte word boundary. The atomic reading and writing is typically bound to CPU's architecture, so the compiler is adding some slack bytes to align your data structure.

    alt text

    Calling #pragma pack(1) may be able to disable it, but not sure if it works on Turbo C.

提交回复
热议问题