class Foo { ... Eigen::Vector2d v; ... }; ... Foo *foo = new Foo;
In other words: you have a class that has as a member a fixed-size vectorizable Eigen object, and then you dynamically create an object of that class.
class Foo { ... Eigen::Vector2d v; ... public: EIGEN_MAKE_ALIGNED_OPERATOR_NEW }; ... Foo *foo = new Foo;
This macro makes "new Foo" always return an aligned pointer.
class Foo { ... Eigen::Vector2d v; ... }; ... Foo *foo = new Foo;
A Eigen::Vector2d consists of 2 doubles, which is 128 bits. Which is exactly the size of a SSE packet, which makes it possible to use SSE for all sorts of operations on this vector. But SSE instructions (at least the ones that Eigen uses, which are the fast ones) require 128-bit alignment. Otherwise you get a segmentation fault.
For this reason, Eigen takes care by itself to require 128-bit alignment for Eigen::Vector2d, by doing two things:
... except in one case. When you have a class Foo like above, and you dynamically allocate a new Foo as above, then, since Foo doesn't have aligned "operator new", the returned pointer foo is not necessarily 128-bit aligned.
The alignment attribute of the member v is then relative to the start of the class, foo. If the foo pointer wasn't aligned, then foo->v won't be aligned either!
The solution is to let class Foo have an aligned "operator new", as we showed in the previous section.
class Foo { double x; Eigen::Vector2d v; public: EIGEN_MAKE_ALIGNED_OPERATOR_NEW };
it will work just fine. You do not need to rewrite it as
class Foo { Eigen::Vector2d v; double x; public: EIGEN_MAKE_ALIGNED_OPERATOR_NEW };
Example:
template<int n> class Foo { typedef Eigen::Matrix<float,n,1> Vector; enum { NeedsToAlign = (sizeof(Vector)%16)==0 }; ... Vector v; ... public: EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) }; ... Foo<4> *foo4 = new Foo<4>; // foo4 is guaranteed to be 128bit-aligned Foo<3> *foo3 = new Foo<3>; // foo3 has only the system default alignment guarantee