Size of packed struct with union of bit fields less than 8 bits in C

筅森魡賤 提交于 2019-12-22 08:42:18

问题


Is it possible in C to get the size of the following structure to be 2?

#include <stdio.h>

struct union_struct {
    char foo;
    char bar : 2;
    union {
        char foobar1 : 6;
        char foobar2 : 6;
    };
};

int main(void)
{
    printf("size of union struct: %d\n", sizeof(struct union_struct));
    return 0;
}

output, compiled with gcc:

size of union struct: 3


回答1:


If you are relying on implementation defined behavior, then yes, but you have to organize it a bit differently:

#ifdef UNNAMED_BITFIELDS_ARE_WELL_DEFINED
#define ANON
#else
#define ANON3(X) anonymous__## X ##__
#define ANON2(X) ANON3(X)
#define ANON ANON2(__LINE__)
#endif

struct union_struct {
    char foo;
    union {
        struct {
            char bar     : 2;
            char ANON    : 6;
        };
        struct {
            char ANON    : 2;
            char foobar1 : 6;
        };
        struct {
            char ANON    : 2;
            char foobar2 : 6;
        };
    };
};

The first byte is foo, the second byte is the anonymous union. Then anonymous union has 3 single byte anonymous structs. Each struct (optionally) uses unnamed bit-fields to allow foobar1 and foobar2 to represent the same 6 bits that follow bar.

From my understanding of the C.11 standard the above code is correct when UNNAMED_BITFIELDS_ARE_WELL_DEFINED is defined. However, there seems to be debate on whether or not unnamed bit-fields have well-defined semantics (see comments below). If unnamed bit-fields do not have well-defined semantics, then the code above can expand each ANON macro into a name for the bit-field.

However, the C.11 standard only defines bit-fields on _Bool, int, and unsigned, while the use of any other type for a bit-field is implementation defined (C.11 §6.7.2.1 ¶5).



来源:https://stackoverflow.com/questions/18647383/size-of-packed-struct-with-union-of-bit-fields-less-than-8-bits-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!