【发布时间】:2011-05-25 15:15:04
【问题描述】:
一行代码抵得上一千字:)这是我的问题:
/* Platform specific 16-byte alignment macro switch.
On Visual C++ it would substitute __declspec(align(16)).
On GCC it substitutes __attribute__((aligned (16))).
*/
#define ALIGN_16 ...
struct ALIGN_16 A {...};
A* ptr = new A;
A* ptr2 = new A[20];
assert(size_t(ptr) % 16 == 0);
for (int i=0; i<20; ++i)
assert(size_t(ptr2+i) % 16 == 0);
assert(sizeof(A) % 16 == 0);
我可以期望所有断言都在支持 SSE 的平台上传递吗?谢谢。
编辑。部分回答。我用 VS2008、GCC 和 ICC 做了一些测试。 MS 编译器确实对齐了 ptr 和 ptr2,但是 GCC 和 ICC 未能对齐 ptr2。
【问题讨论】:
-
简直想不通。我在徘徊,如果数组的每个元素也对齐。
-
标准保证正确分配数组的元素与相关类型正确对齐。然而,这种对齐是一个实现细节,理论上可能是一个字节(即打包对齐)。
标签: c++ gcc alignment icc visual-studio