Boost logo

Ublas :

From: Vardan Akopian (vakopian_at_[hidden])
Date: 2005-12-29 14:17:31


On 12/28/05, Dima Sorkin <dsorkin_at_[hidden]> wrote:
> Quoting "Harris, Chris GOSS EXT" :
> > Maybe matrix should behave like std::vector and always default
> > initialize its values.
> Hi.
> I don't understand the difference...
> I always thought that in C++ "uninitialized" and
> "default constructed" is the same, as ( at high level )
> for each "unitialized" object default constructor is called anyway,
> and then the call is simply optimized out if possible.

That's only true for non-POD's, i.e. primitives such as int and double
are never default initialized in C++. So for matrix<double> the
internal array of double's is not default initialized. In STL though,
it's part of the std::vector concept, that the internal array is
initialized.
I agree with Chris Harris: it looks like as of now there is no way to
resize a matrix preserving the existing elements, but not initializing
the new ones.
However I think ublas made the correct choice of not default
initializing vector and matrix values in constructors. In many
situations, in numerical algorithms you need to fully populate the
matrix in your code, so having the (potentially big) matrix default
initialized would be a waste.
I think, one solution could be to add another constructor and resize
function, e.g.
matrx::matrix(size_type size1, size_type size2, value_type init_value);
matrix::resize(size_type size1, size_type size2, bool preserve,
value_type init_value);

But for now, if you need to 0-initialize the container, you'll need to
call clear() after construction.

-Vardan

P.S. Notice that compressed matrix acts as if it was default
initialized in constructor. This is ok since there is no waste in
doing so.