我想对来自另一个库的数据使用 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
这是相当不方便的,因为存在cache
. 另一个没有这个问题:
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
来释放其数据的所有权(自行分配或采用)。