Compiler Warning when using Pointers to Packed Structure Members Compiler Warning when using Pointers to Packed Structure Members c c

Compiler Warning when using Pointers to Packed Structure Members


clang has just added a new -Waddress-of-packed-member warning for this specific issue. See https://reviews.llvm.org/rL278483 and https://llvm.org/bugs/show_bug.cgi?id=22821 for details. It should appear in the 4.0 release of clang.


gcc has just added the same warning (-Waddress-of-packed-member) in gcc-9. It is enabled by default when using -Wall


consider following alignment for the typedef structure definition:

#pragma pack(1)typedef struct { int x; } uas __attribute__ ((aligned (sizeof(uint32_t))));#pragma pack()

this will notify compiler to make sure all allocations of the structure will be 4 bytes aligned.

the reference is here