Eigen的变通方法:矩阵以发布数据
workaround for Eigen::Matrix to release data
我想对来自另一个库的数据使用Eigen3。ggael的早期回答指示了Eigen::Matrix
采用具有new
关键字的预存数据的方法。然而,这对我来说还不够,因为生成的Matrix
似乎仍然获得了数据的所有权,这意味着它将在超出范围时释放数据。也就是说,如果data
最终被其来源的库删除,这将是一个崩溃:
void crasher(double* data, size_t dim)
{
MatrixXd m;
new (&m) Map<MatrixXd>(data,dim,dim); // matrix adopts the passed data
m.setRandom(); cout<<m<<endl; // manipulation of the passed data via the Matrix interface
} // data deleted by m => potential problem in scope of function call
我想出了两个解决办法:
void nonCrasher1(double* data, size_t dim)
{
MatrixXd m; // semantically, a non-owning matrix
const Map<const MatrixXd> cache(m.data(),0,0); // cache the original „data” (in a const-correct way)
new (&m) Map<MatrixXd>(data,dim,dim); // matrix adopts the passed data
m.setRandom(); cout<<m<<endl; // manipulation of the passed data via the Matrix interface
new (&m) Map<const MatrixXd>(cache); // re-adopting the original data
} // original data deleted by m
由于CCD_ 5的存在,这是相当不方便的。另一个没有这个问题:
void nonCrasher2(double* data, size_t dim) // no need for caching
{
MatrixXd m; // semantically, a non-owning matrix
new (&m) Map<MatrixXd>(data,dim,dim); // matrix adopts the passed data
m.setRandom(); cout<<m<<endl; // manipulation of the passed data via the Matrix interface
new (&m) Map<MatrixXd>(nullptr,0,0); // adopting nullptr for subsequent deletion
} // nullptr „deleted” by m (what happens with original data [if any]?)
然而,这里还不清楚m
的原始数据会发生什么(如果有的话——所有这些从Eigen3的文档中还不完全清楚)。
我的问题是Eigen::Matrix
是否有一种规范的方式来释放其数据的所有权(自行分配或采用)。
在nonCrasher2
中,除了故意用随机值填充之外,数据不会发生任何变化。
然而,这看起来仍然很粗糙,干净的方法是使用Map
对象而不是MatrixXd
:
Map<MatrixXd> m(data, dim, dim);
m.setRandom();
如果您需要m
成为MatrixXd
,因为您需要调用采用MatrixXd
对象的函数,而这些函数无法模板化,那么您可以考虑将这些函数泛化为采用Ref<MatrixXd>
对象。默认情况下,Ref<MatrixXd>
可以接受存储器重新组装为具有任意前导维度的MatrixXd
的任何表达式。它已在特征3.2中介绍,请查看文档以了解更多详细信息。
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- 数据成员SFINAE的C++17测试:gcc vs clang
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何对点云数据进行排序
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- 在运行时处理类型擦除的数据-如何不重新发明轮子