C++ gcc alignas问题与指向对象的成员指针
我正在使用GCC4.9.2,我正在尝试正确地对齐静态初始化的数组,以便与AVX一起使用。以下是因对齐问题导致故障的代码要点:C++ gcc alignas问题与指向对象的成员指针,c++,c++11,memory-alignment,alignas,C++,C++11,Memory Alignment,Alignas,我正在使用GCC4.9.2,我正在尝试正确地对齐静态初始化的数组,以便与AVX一起使用。以下是因对齐问题导致故障的代码要点: #include <iostream> #include <cstddef> struct B { alignas(32) double x[1] = {0}; }; struct A { A() { b1 = new B(); b2 = new B(); } B* b1; B* b2; }; int mai
#include <iostream>
#include <cstddef>
struct B {
alignas(32) double x[1] = {0};
};
struct A
{
A() { b1 = new B(); b2 = new B(); }
B* b1;
B* b2;
};
int main(int argc, char** argv) {
A a;
int ret = (ptrdiff_t) a.b1->x % 32 + (ptrdiff_t) a.b2->x % 32;
std::cout << (ptrdiff_t) a.b1->x % 32 << "," << (ptrdiff_t) a.b2->x % 32 << "\n";
return ret;
}
#包括
#包括
结构B{
alignas(32)双x[1]={0};
};
结构A
{
A(){b1=新B();b2=新B();}
B*b1;
B*b2;
};
int main(int argc,字符**argv){
A A;
int ret=(ptrdiff_t)a.b1->x%32+(ptrdiff_t)a.b2->x%32;
std::cout x%32如果我理解正确,请参阅以下文档
new
不需要遵守alignas
注意,您可以实现一个自定义的操作符new
,调用posix_memalign
。我还在gcc bugzilla中发布了一个bug,并得到了类似的答案: