很抱歉有一个愚蠢的问题,但是如果我需要确保结构/类/union 的对齐,是否应该在typedef声明中添加属性((aligned(align)))?
class myAlignedStruct{} __attribute__ ((aligned(16)));
typedef myAlignedStruct myAlignedStruct2; // Will myAlignedStruct2 be aligned by 16 bytes or not?
最佳答案
否... typedef只是用于指定实际类型的假名或别名,它们不作为单独的类型存在,以具有不同的对齐方式,打包方式等。
#include <iostream>
struct Default_Alignment
{
char c;
};
struct Align16
{
char c;
} __attribute__ ((aligned(16)));
typedef Align16 Also_Align16;
int main()
{
std::cout << __alignof__(Default_Alignment) << '\n';
std::cout << __alignof__(Align16) << '\n';
std::cout << __alignof__(Also_Align16) << '\n';
}
输出:
1
16
16
关于c++ - GCC中的结构对齐方式(应该在typedef中指定对齐方式吗?),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6786138/