C++ – Specifying 64-bit Alignment with GCC

alignmentc++gcc

Given a structure definition like

struct foo {
    int a, b, c;
};

What's the best (simplest, most reliable and portable) way to specify that it should always be aligned to a 64-bit address, even on a 32-bit build? I'm using C++11 with GCC 4.5.2, and hoping to also support Clang.

Best Answer

Since you say you're using GCC and hoping to support Clang, GCC's aligned attribute should do the trick:

struct foo {
    int a, b, c;
} __attribute__((__aligned__(8))); // aligned to 8-byte (64-bit) boundary
Related Question