I wrote the following code and tested it on gcc:
#if _MSC_VER
#define ALN_BEGIN(x) __declspec(align(x))
#define ALN_END(x)
#define alignof(x) __alignof(x)
#else
#define ALN_BEGIN(x)
#define ALN_END(x) __attribute__((aligned(x)))
#define alignof(x) __alignof__(x)
#endif
ALN_BEGIN(32) struct Foo
{
float a;
float b;
float c;
} ALN_END(32);
int main(int argc, char** argv)
{
printf("size %d, alignment%d\n", sizeof(Foo), alignof(Foo));
}
When I compile it with gcc and run, although Foo has only 12 bytes for all its members, I sizeof(Foo)got 32, the alignment size. Is the size expandable by the language standard (which is reliable) or is it just a feature for GCC?
I am creating an object pool class, so I need to work exactly with size and type alignment.
source
share