I thought the "aligned" attribute will always be respected. But I tried the code sample below, and the output was "16 19". I was expecting "16 24". I used g++. Why the compiler ignored the "aligned" attribute in this case? Does "packed" take precedence over "aligned"? Is there a way to make sure Test1 is always properly aligned to 8?
typedef unsigned char uint8;
typedef unsigned int uint32;
typedef unsigned long long uint64;
class Test1
{
public:
uint8 t1;
uint64 t2;
} __attribute__ ((aligned(8)));
class Test2
{
private:
uint8 t1;
uint8 t2;
uint8 t3;
Test1 t4;
} __attribute__ ((packed));
int
main(int argc, char **argv)
{
Test1 t1;
Test2 t2;
printf("%ld %ld\n", sizeof(t1), sizeof(t2));
}