GCC: Alignment settings with __attribute__ and #pragma

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 09:16:09

问题


How can I define #pragma pack(2) as a structure attribute?

I've read here that __attribute__((packed,aligned(4))) is roughly equivalent to #pragma pack(4).

However, if I try to use this (at least with 2 instead of 4), I get different results. Example:

#include <stdio.h>

#pragma pack(push, 2)
struct test1 {
    char a;
    int b;
    short c;
    short d;
};
struct test1 t1;
#pragma pack(pop)

struct test2 {
    char a;
    int b;
    short c;
    short d;
} __attribute__((packed,aligned(2)));
struct test2 t2;


#define test(s,m) printf(#s"::"#m" @ 0x%04x\n", (unsigned int ((char*)&(s.m) - (char*)&(s)))
#define structtest(s) printf("sizeof("#s")=%lu\n", (unsigned long)(sizeof(s)))

int main(int argc, char **argv) {
    structtest(t1);
    test(t1,a);
    test(t1,b);
    test(t1,c);
    test(t1,d);

    structtest(t2);
    test(t2,a);
    test(t2,b);
    test(t2,c);
    test(t2,d);
}

Output is (compilation on x86 or x86x64, Linux, gcc 4.8.4):

sizeof(t1)=10
t1::a @ 0x0000
t1::b @ 0x0002
t1::c @ 0x0006
t1::d @ 0x0008
sizeof(t2)=10
t2::a @ 0x0000
t2::b @ 0x0001
t2::c @ 0x0005
t2::d @ 0x0007

The adresses of members b, c and d are not the same in both cases.

Is there another __attribute__ I have to add? I can't even find a fine granular documentation on these attributes in the gcc documentation.


回答1:


As noted by @Nominal Animal in the comments the solution is to add __attribute__((__aligned__(s))) to each struct member as it can be used to set the alignment individually for each member.



来源:https://stackoverflow.com/questions/43135390/gcc-alignment-settings-with-attribute-and-pragma

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