I’m trying to pack data in a c++ struct.
My struct has this layout:
struct structName
{
int16_t member1;
int32_t member2;
uint32_t member3;
uint32_t member4;
uint32_t member5;
etc
}__attribute__((packed));
Using offsetof($structname, $membername) I get back the correct offsets of the data (0,2,6,10,14 . . .), but when I access the data by member-name I get the data at 4 byte offsets (0,4,8,12,16 . . .) as if the struct wasn’t packed.
Is
} __attribute__((packed));
the correct way to make a struct packed?
.
.
Update: mydogisbox wrote:
__attribute__((packed))is a gcc extension, which is supported.The clang documentation says it also supports
#pragma pack(...)directive:source: http://clang.llvm.org/docs/UsersManual.html
Just say:
to see if it works (compile with
-fms-extensionsif not using Windows).Note the above are all non-standard extensions, and the new C++11 standard has a new
alignaskeyword: http://en.cppreference.com/w/cpp/language/alignasbut its support is still a bit sketchy.