Boost logo

Boost Users :

Subject: Re: [Boost-users] Multiarray resize
From: Alfredo Correa (alfredo.correa_at_[hidden])
Date: 2009-08-11 20:21:09


> The function multi_array::resize() returns a multi_array reference.
> Does it mean that resize can change the memory location of the
> current multi_array object?

No, the multi-array object it self does not change it position in
memory, if you read the code it returns simply (*this) after swapping
member fields with another object.

But, what can change is the memory location of the actual data, since
the data sometimes (always?) is reallocated and copied to the new
location. That means that the return value of origin() (i.e. pointer
to the first element in memory) can change after resize.

Like you, I don't know why the author choose to return a reference to
(*this) after resize. Maybe it is to be able to easily keep track of
the pointer to memory, as follows

  boost::multi_array A(....);
  double* actual_data=A.origin();
  ...
  actual_data=A.resize(...).origin();

But, to be honest, I don't know if this is compelling enough to
actually provide such return behavior.

Another reason to do this is that, suppose that you want to resize the
matrix but you are not interested in keeping the elements as resize
tries to do. If you use resize directly then there is a waste in
copying elements that you are not interested in, then I am pretty sure
that it doesn't try to copy elements if you do:

  boost::multi_array A(extents[5][5]);
  A.resize(extents[0][0]).resize(extents[10][10]); //equivalent to
A.resize(extents[10][10]); but elements are not preserved.

As I tell you, these are wild guesses, I wish one of the authors can
explain the reason for that.

> can I just do
> my_array->resize()

yes, this is enough in my opinion.

> or I should do
> my_array=my_array->resize()

this is not necessary.

Cheers,
Alfredo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net