我认为“对齐”属性将始终受到尊重。但我尝试了下面的代码示例,输出为“16 19”。我期待“16 24”。我用过g ++。为什么编译器在这种情况下忽略了“aligned”属性? “打包”优先于“对齐”吗?有没有办法确保Test1始终与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));
}