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
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:
A.resize(extents).resize(extents); //equivalent to
A.resize(extents); 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
yes, this is enough in my opinion.
> or I should do
this is not necessary.
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