关于Eigen使用时的bug

今天编译完成lidar_project后运行程序,出现下面的bug提示

back_end_node: /usr/include/eigen3/Eigen/src/Core/DenseStorage.h:128: Eigen::internal::plain_array<T, Size, MatrixOrArrayOptions, 32>::plain_array() [with T = float; int Size = 16; int MatrixOrArrayOptions = 0]: Assertion `(internal::UIntPtr(eigen_unaligned_array_assert_workaround_gcc47(array)) & (31)) == 0 && "this assertion is explained here: " "http://eigen.tuxfamily.org/dox-devel/group__TopicUnalignedArrayAssert.html" " **** READ THIS WEB PAGE !!! ****"' failed.
之前也见过Eigen的bug,基本是因为内存管理的问题,使用容器里面是eigen类型的变量,需要在变量声明时主动声明内存,类似于下面这段,这次这个问题给了个提示的网站,原来开发者也知道有这样的问题,提前做了预案,一共四种情况,我们一项一项看看。写在前面,文章里说用c++17就可以避免这样的问题。

解决方案:

调整了几天,即使按照情况1来改也没有解决这个问题,最后看到个帖子关于使用share_ptr和Eigen的,终于解决了这个问题,具体改动如下:

    // std::shared_ptr<BackEndFlow> back_end_flow_ptr = std::make_shared<BackEndFlow>(nh);
    std::shared_ptr<BackEndFlow> back_end_flow_ptr = std::shared_ptr<BackEndFlow>(new BackEndFlow(nh));

有文章里说是因为make_shared()这种方式还是忽略了重载中的operater new,所以需要显示调用类重载的operater new。另外,因为一个node程序会依赖很多个子程序,子程序中使用eigen和智能指针的都需要进行改动。

参考的网址就是提示中的

Eigen: Explanation of the assertion on unaligned arrays

std::vector<Eigen::Vector4f,Eigen::aligned_allocator<Eigen::Vector4f> >

1、结构体或者类内有Eigen的对象

如下所示,类内有vector4d的Eigen类型对象。

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

对于这种情况,需要在类内的public中写个宏,算是重载了new(),如下

class Foo
{
  ...
  Eigen::Vector4d v;
  ...
public:
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};
 
Foo *foo = new Foo;

2、 使用了标准的容器STL Containers or manual memory allocation

These issues arise only with fixed-size vectorizable Eigen types and structures having such Eigen objects as member. For other Eigen types, such as Vector3f or MatrixXd, no special care is needed when using STL containers.

使用了像vector这样的容器,像下面这样

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

改成

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

3、函数直接使用Eigen对象传值

void func(Eigen::Vector4d v);

上面这样的改成引用

void my_function(const Eigen::Vector2d& v);

如果是类或者结构体类内有Eigen变量,那么需要用该类时需要如下:

struct Foo
{
  Eigen::Vector2d v;
};
//void my_function(Foo v);
void my_function(const Foo& v);

4、for instance GCC on Windows

Eigen: Explanation of the assertion on unaligned arrays

参考链接:

Eigen 与std::make_shared,std::unique_ptr 搭配使用的采坑记录 - 知乎

c++11 - Eigen::aligned_alocator & std::shared_ptr - Stack Overflow

std::make_shared fails to fulfill structure aliment (#1049) · Issues · libeigen / eigen · GitLab

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Eigen 是一个 C++ 模板库,用于线性代数和数值计算。它提供了高性能的矩阵和向量操作,以及许多常用的数学函数和算法。下面是 Eigen 库的基本使用说明: 1. 安装:你可以从 Eigen 的官方网站(https://eigen.tuxfamily.org/)下载最新版本的源代码,并将其包含在你的项目中。 2. 包含头文件:在你的 C++ 代码中,使用 `#include <Eigen/Dense>` 来包含 Eigen 的主要头文件。 3. 定义矩阵和向量:Eigen 提供了 `Matrix` 和 `Vector` 类模板来定义矩阵和向量。你可以使用不同的数据类型(例如 float、double)和大小来实例化这些类。 ```cpp #include <Eigen/Dense> int main() { // 定义一个 3x3 的矩阵 Eigen::Matrix3d matrix; // 定义一个 3 维向量 Eigen::Vector3d vector; // ... return 0; } ``` 4. 矩阵和向量操作:Eigen 提供了丰富的运算符和函数,用于执行矩阵和向量的各种操作,如矩阵乘法、转置、逆等。 ```cpp #include <Eigen/Dense> #include <iostream> int main() { Eigen::Matrix2d A; Eigen::Vector2d b; // 矩阵赋值 A << 1, 2, 3, 4; // 向量赋值 b << 5, 6; // 矩阵乘法 Eigen::Vector2d result = A * b; // 输出结果 std::cout << result << std::endl; return 0; } ``` 5. 其他功能:Eigen 还提供了许多其他功能,如特征值分解、奇异值分解、QR 分解等。你可以通过查阅 Eigen 的官方文档来了解更多详细的使用方法和示例代码。 以上是 Eigen 库的基本使用说明,希望对你有所帮助!如有更多问题,请随提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值