我想定义一个结构体,以便 sizeof(myStruct) = 12
和 __alignof(myStruct) = 8
。
struct myStruct{
int32_t a;
int32_t b;
int32_t c;
};
这可能吗?
目前我必须手动执行以下操作:
__declspec(align(8)) myStruct str1; // Assume str1 start at 0x00
__declspec(align(8)) myStruct str2; // str2 address is 0x10
__declspec(align(8)) myStruct str3; // str3 address is 0x20
double abc; // abc address is 0x2C
__declspec(align(8)) myStruct str4; // str4 address is 0x38
最佳答案
对象的大小必须是其对齐要求的倍数。 8 不是 12 的因数。因此,不可以,您不能使用大小为 12 且对齐方式为 8 的类型。
您可以使用 alignas
说明符请求比类的子对象所需的更严格的对齐方式,并且类的大小将相应增加。您的示例类将被填充到 16 个字节。
关于c++ - 结构大小 12,结构对象对齐到 8,可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43010020/