使用Eigen库和stl容器时遇到问题

来源:互联网 发布:sql语句时间格式化 编辑:程序博客网 时间:2024/06/07 02:21

在程序中使用了这样的容器

std::vector<Eigen::Matrix4f> p;
由于Eigen自身分配空间方法与stl空间分配的问题,在执行push_back()操作时,有时会弹出如下的错误信息,并导致程序崩溃。

Assertion failed: (reinterpret_cast<size_t>(array) & 0xf) == 0 && "this assertion is explained here: " "http://eigen.tuxfamily.org/dox-devel/group__TopicUnalignedArrayAssert.html" " **** READ THIS WEB PAGE !!! ****"

根据提示访问了http://eigen.tuxfamily.org/dox-devel/group__TopicUnalignedArrayAssert.html网页,网页中显示可能的原因如下:

Cause 1: Structures having Eigen objects as members

If you have code like this,

class Foo
{
//...
Eigen::Vector2d v;
//...
};
//...
Foo *foo = new Foo;

then you need to read this separate page: Structures Having Eigen Members.

Note that here, Eigen::Vector2d is only used as an example, more generally the issue arises for all fixed-size vectorizable Eigen types.

Cause 2: STL Containers or manual memory allocation

If you use STL Containers such as std::vector, std::map, ..., with Eigen objects, or with classes containing Eigen objects, like this,

std::vector<Eigen::Matrix2f> my_vector;
struct my_class { ... Eigen::Matrix2f m; ... };
std::map<int, my_class> my_map;

then you need to read this separate page: Using STL Containers with Eigen.

Note that here, Eigen::Matrix2f is only used as an example, more generally the issue arises for all fixed-size vectorizable Eigen types and structures having such Eigen objects as member.

The same issue will be exhibited by any classes/functions by-passing operator new to allocate memory, that is, by performing custom memory allocation followed by calls to the placement new operator. This is for instance typically the case of std::make_shared or std::allocate_shared for which is the solution is to use an aligned allocator as detailed in the solution for STL containers.

Cause 3: Passing Eigen objects by value

If some function in your code is getting an Eigen object passed by value, like this,

void func(Eigen::Vector4d v);

then you need to read this separate page: Passing Eigen objects by value to functions.

Note that here, Eigen::Vector4d is only used as an example, more generally the issue arises for all fixed-size vectorizable Eigen types.

Cause 4: Compiler making a wrong assumption on stack alignment (for instance GCC on Windows)

This is a must-read for people using GCC on Windows (like MinGW or TDM-GCC). If you have this assertion failure in an innocent function declaring a local variable like this:

void foo()
{
Eigen::Quaternionf q;
//...
}

then you need to read this separate page: Compiler making a wrong assumption on stack alignment.

Note that here, Eigen::Quaternionf is only used as an example, more generally the issue arises for all fixed-size vectorizable Eigen types.


然后跳转到网页http://eigen.tuxfamily.org/dox-devel/group__TopicStlContainers.html,找到了正确的使用方法:

The case of std::vector

The situation with std::vector was even worse (explanation below) so we had to specialize it for the Eigen::aligned_allocator type. In practice you must use the Eigen::aligned_allocator (not another aligned allocator), and #include <Eigen/StdVector>.

Here is an example:

#include<Eigen/StdVector>
/* ... */
std::vector<Eigen::Vector4f,Eigen::aligned_allocator<Eigen::Vector4f> >