[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#93481: pragma pack works on x86; ignored on arm



There is no bug here.  "#pragma pack" isn't ignored on arm, it just doesn't 
do what you expect it to.  Try this; the second printf should yield the same 
results on x86 and arm.

#pragma pack(2)
struct foo {
        short a;
        long b;
};

int main() {
        struct foo f;
        /* prints 6 on x86, 8 on arm */
        printf("%d\n", sizeof(struct foo));
        /* prints 2 with "pragma pack", 4 without */
        printf("%d\n", (int)&f.b - (int)&f);
        return 0;
}

The effect of `pragma pack' is to turn off intra-structure padding.  The 
reason that sizeof() returns 8 on ARM is due to a different, and stronger, 
constraint: namely that the machine ABI specifies all structures must be 
32-bit aligned.  If you really need to override this, marking the whole 
structure "attribute ((packed))" will do the trick.

If the GCC documentation suggests that "pragma pack" and "attribute 
((packed))" are supposed to be synonymous, the documentation is wrong.

p.





Reply to: