使用std :: vector和包含在单独文件中定义的特征类型的类/结构的问题

时间:2018-09-12 10:05:10

标签: c++ vector std eigen

编辑>>结论,由于糟糕的本地配置而导致的本征版本冲突? <<< / p>

我正在尝试定义一个包含Eigen :: Matrix的结构。 当我使用std :: vector并使用某些函数填充数组时,会发生一些奇怪的事情。

编辑>> <<<< / p>下的完整源代码

当我在同一个文件中定义所有内容时,就像上面没有问题。填充向量并正确返回,调用size()访问所得向量很好。 (size()= 10)。

当我在单独的.h和.cpp文件中定义main的类型和函数并执行相同的操作时,返回的数据将移位,以使size()返回12。

看看矩阵元素,我可以看到数据向向量中的每个对象向右移动了1个元素。

在单独的文件中定义时,使它起作用的唯一方法是将矩阵声明为

Eigen::Matrix<float, 4, 4, Eigen::DontAlign> tf

但是我想这对以后的效率不好。 任何想法是什么问题?我认为这是由于分配时对齐矩阵引起的,但是为什么从不同的源文件进行编译/链接时这会成为问题?

Ubuntu 16.04 gcc 5.4.0 C ++标准11 Eigen 3.2.1(由GTSAM slam软件包提供)

编辑->>>>

完整的源代码:

key_frames.h

#ifndef __KEY_FRAMES_H__
#define __KEY_FRAMES_H__

#include <Eigen/Dense>

struct test_t {
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
    Eigen::Matrix<float, 4, 4> tf;
    int key;
};

void load_test_v(std::vector<test_t,
             Eigen::aligned_allocator<test_t> > & test_vector);
#endif

key_frames.cc

#include <vector>

#include "key_frames.h"

void load_test_v(std::vector<test_t,
             Eigen::aligned_allocator<test_t> > & test_vector){

     for (int i = 0; i < 10; i++) {
     test_t tt;
     tt.tf = Eigen::Matrix4f::Random();
     tt.key = i;
     test_vector.push_back(tt);
    }
 }

opt_key_frames.cc

#include <iostream>

#include <gtsam/geometry/Pose2.h>

#include "key_frames.h"

int main(int argc, const char** argv) {
   std::vector<test_t,
          Eigen::aligned_allocator<test_t> > test_vector;
   test_vector.clear();
   std::cout << "size of test vector " << test_vector.size() << "\n";
   load_test_v(test_vector);
   std::cout << "size of test vector " << test_vector.size() << "\n";
}

这是最小的可复制来源。要构建,我链接到gtsam库。 有趣的是,如果我删除了

#include <gtsam/geometry/Pose2.h>

代码有效(size()返回10)。 另外,如果我将确切的代码复制并制作文件到其他目录,则它可以正常工作。

从valgrid输出:

--14269-- REDIR: 0x5c0abb0 (libc.so.6:bcmp) redirected to 0x4a28770 (_vgnU_ifunc_wrapper)
--14269-- REDIR: 0x5cea430 (libc.so.6:__memcmp_sse4_1) redirected to 0x4c33780 (__memcmp_sse4_1)
size of test vector 0
--14269-- REDIR: 0x5c03600 (libc.so.6:posix_memalign) redirected to 0x4c2fd12 (posix_memalign)
size of test vector 12
==14269== 
==14269== HEAP SUMMARY:
==14269==     in use at exit: 72,736 bytes in 2 blocks
==14269==   total heap usage: 259 allocs, 257 frees, 98,174 bytes allocated
==14269== 
==14269== Searching for pointers to 2 not-freed blocks
==14269== Checked 2,634,840 bytes
==14269== 
==14269== LEAK SUMMARY:
==14269==    definitely lost: 0 bytes in 0 blocks
==14269==    indirectly lost: 0 bytes in 0 blocks
==14269==      possibly lost: 0 bytes in 0 blocks
==14269==    still reachable: 72,736 bytes in 2 blocks
==14269==         suppressed: 0 bytes in 0 blocks
==14269== Reachable blocks (those to which a pointer was found) are not shown.

如果删除gtsam,则包括:

--14359-- REDIR: 0x54613b0 (libc.so.6:__GI_mempcpy) redirected to    0x4c34a90 (__GI_mempcpy)
size of test vector 0
--14359-- REDIR: 0x5459600 (libc.so.6:posix_memalign) redirected to 0x4c2fd12 (posix_memalign)
--14359-- REDIR: 0x54564f0 (libc.so.6:free) redirected to 0x4c2ec69 (free)
size of test vector 10
==14359== 

所以那里正在发生变化。

结论:我的设置搞砸了。 我怀疑包括Eigen / Dense在内的系统都针对Eigen3.3安装的系统。尽管GTSam使用了该软件包附带的Eigen 3.2,但由于某种原因,我的本地设置使它们感到困惑,而当我在其他目录中运行代码时,只会使用GTSam Eigen。

无论如何,感谢您的帮助。通过它的工作,帮助我理解了。

0 个答案:

没有答案