In my project I have written the following code:
Eigen::ArrayXf foo(const Eigen::ArrayXcf &src)
{
auto * output = new float[src.size()];
//Fill the data here
return Eigen::Map<Eigen::ArrayXf>(output, src.size());
}
Note that the stuff constructed in return statement is an Eigen::Map
, but compiler does not complain anything so there must be a conversion. Therefore I have the following questions:
- How does this conversion happen? By making a deep copy of original data or just changing ownership?
- Is this code OK? Does it cause undefined behaviours or memory leaks?
- If I want the returned
Eigen::Array
owns the data in*output
instead of copying it, and release this block of memory on destruction, how can I achieve this? At here I noticedEigen::Array
andEigen::Matrix
can be initialized with raw data pointer, but it seems only work on fixed size arrays and matrices. This function is going to be called frequently so I do care about the efficiency.