Boost logo

Ublas :

From: Michael Stevens (mail_at_[hidden])
Date: 2005-08-26 11:22:12


On Friday 19 August 2005 14:36, Peter Schmitteckert wrote:
> Salut,
>
> On Fri, 19 Aug 2005, Gunter Winkler wrote:
>
> [...]
>
> > ALLOC::size_type, T, bool) [with T = int, ALLOC =
>
> std::allocator<int>]«:
> > boost/numeric/ublas/storage.hpp:110: Warnung: `int*data'
> > might be used uninitialized in this function
> >
> >
> > We should change line 110
> > pointer data = 0;
> >
> > otherwise data_ will have random content if the new size is zero.
> > (Alternatively we can add an else clause to "if (size) { ... }" )

This is annoying. In unbounded_array value of data_ can be undefined when
size_ is 0. We only need to maintain the invariant that data_ points to the
storage when size_ !=0.

> If it's never referenced is not a Problem.
> Never being referenced means not before an initialization.
>
> BTW, we should use
> pointer data = NULL;

Stroustrup in 5.1.1 'Zero' recommends 0!

> I'd like to remark, that pointer may not have random content,
> but it's use it undefined.

Not sure I understand this sentance.

>
> e.g. a code like
>
> double a;
> double b;
>
> b=a;
> b=1.0;
>
> std::cout << b << std::endl;
>
> is undefined and can (and will on some compilers with -O3)
> lead to crashes somewhere else in the code.

The initialised assignment is only a problem for float types. However for
pointers I believe the following is valid?

  pointer data;
  if (condition) {
        // initialise data
  }
  // dereference data_
  data_ = data;

That is, it is valid to assign an initialised pointer to another.

This I believe is the most efficient implementation. Which is important as
unbounded_array is used a lot. Trouble is compilers warn about the possible
uninitialised assignment.

I really do want to have:
  pointer data;
  if (condition) {
        // initialise data
  }
  // dereference data_
  if (condition)
          data_ = data;

as condition == true is the common branch case.

Maybe on modern processors the extra condition is no more costly then the
unnecessary assignment however?

I have committed an untested change which makes the initialised construction
of data_ consistent. It also adds zeroing of data_ if !NDEBUG to simplify
debugging. I doesn't address the initialised assignment warning in resize
however.

Michael

-- 
___________________________________
Michael Stevens Systems Engineering
34128 Kassel, Germany
Phone/Fax: +49 561 5218038
Navigation Systems, Estimation  and
                 Bayesian Filtering
    http://bayesclasses.sf.net
___________________________________