我想对来自另一个库的数据使用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中介绍,请查看文档以了解更多详细信息。