A dynamically sized Eigen::Matrix holds its values in a continuous memory block. I need these values as a memory block I own. I currently copy the values over using std::memcpy.
#include <cstdlib>
#include <cstring>
#include <eigen3/Eigen/Core>
using RowMajorMatrixXf = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
int main()
{
RowMajorMatrixXf mat(1024, 2048);
// ...
const std::size_t num_bytes = mat.rows() * mat.cols() * sizeof(float);
float* ptr = (float*)std::malloc(num_bytes); // raw ptr for simplicity
std::memcpy(ptr, mat.data(), num_bytes);
// ...
std::free(ptr);
}
However the copying is unnecessary, since the Eigen::Matrix is no longer needed at this point. How can I acquire the ownership of the memory of the Eigen Matrix, essentially preventing the Matrix object from freeing the memory in its destructor?
Aucun commentaire:
Enregistrer un commentaire