2
votes

I am using a boost::multi_array to store some data. I do all my work on the data using views, because I need to work with slices of the data in different dimensions.

My question is, how is the memory of boost::multi_array managed?

In particular, I would like to work with a copy of a slice (view) of my data. Therefore I would like to do something like this:

typedef boost::multi_array<double, 2> MyContainer;
typedef MyContainer::array_view<1>::type Slice1D;

Slice1D copy(const Slice1D & slice)
{
    MyContainer copyMemory(slice);
    Slice1D sliceCopy = copyMemory[boost::indices[0][range()]];
    return sliceCopy;
}

void main()
{
    ...
    Slice1D copySlice = copy(mySlice);
}

I would like to know whether this is wrong because the multi_array containing the copy goes out of scope when the copy function returns. Or is it OK because multi_array uses smart pointers or reference counting to keep track?

1

1 Answers

1
votes

multi_array owns the memory.

array_view does not own the memory.

There is no sharing of ownership.

Your code is not gonna work well.