Opened 10 years ago

Last modified 9 years ago

#8438 new Bugs

vector & circular_buffer storage misbehave when using compiler optimizations — at Version 3

Reported by: ofir Owned by: Jan Gaspar
Milestone: To Be Determined Component: uBLAS
Version: Boost 1.52.0 Severity: Problem
Keywords: Cc: Douglas Gregor, Jan Gaspar, ofirmebel@…

Description (last modified by viboes)

When compiling the following code without optimizations, it behaves as expected: (compiled with g++-4.7.2 with no flags at all)

#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/circular_buffer.hpp>

int main () {
    boost::numeric::ublas::vector<double, boost::circular_buffer<double> > v (3, 1);
    std::cout << v << std::endl;
    v[1] = 5;
    std::cout << v << std::endl;
    std::cout << v[1] << std::endl;
    return 0;
}

Output:

[3](1,1,1)
[3](1,5,1)
5

When compiling the exact same code with O1, O2, and O3 it produces the following output:

[3](0,0,0)
[3](0,0,0)
5

I noticed that inner_prod() also sees the vector as zeros.

Change History (3)

comment:1 by ofir <ofirmebel@…>, 10 years ago

Cc: ofirmebel@… added

comment:2 by Marshall Clow, 10 years ago

I was able to replicate this with clang, but I got a warning which probably has some bearing here:

trunk/boost/numeric/ublas/vector.hpp:205:20: warning: returning reference to
      local temporary object [-Wreturn-stack-address]
            return data () [i];
                   ^~~~~~~~~~~
trunk/boost/numeric/ublas/io.hpp:58:18: note: in instantiation of member
      function 'boost::numeric::ublas::vector<double,
      boost::circular_buffer<double, std::allocator<double> > >::operator()'
      requested here
            s << v () (0);
                 ^
junk.cpp:7:15: note: in instantiation of function template specialization
      'boost::numeric::ublas::operator<<<char, std::char_traits<char>,
      boost::numeric::ublas::vector<double, boost::circular_buffer<double,
      std::allocator<double> > > >' requested here
    std::cout << v << std::endl;
              ^
1 warning generated.

comment:3 by viboes, 9 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.