[Bug c++/44069] New: optimization bug initializing from cast array

kraftche at cae dot wisc dot edu gcc-bugzilla@gcc.gnu.org
Tue May 11 00:00:00 GMT 2010


The code below, when compiled with out optimization, produces the expected
result:

% g++ bug.cc -o bug
% ./bug
{1, 2, 3, 4}
{5, 6, 7, 8}
{9, 0, 1, 2}
{3, 4, 5, 6}

When compiled with optimization, it produces garbage for all rows of the matrix
except the first row:

% g++ -O2 bug.cc -o bug
% ./bug
{1, 2, 3, 4}
{3.11042e-317, 2.07381e-317, 4.94066e-323, 2.07372e-317}
{0, 2.07321e-317, 6.92543e-310, 2.07375e-317}
{6.92543e-310, 2.07372e-317, 0, 2.0733e-317}

bug.cc:

#include <iostream>

template <unsigned R, unsigned C>
class M {
  public:
    M( const double* arr ) {
      for (unsigned r = 0; r < R; ++r)
        for (unsigned c = 0; c < C; ++c)
          m[r*C+c] = arr[r*C+c];
    }
    double operator()(unsigned r, unsigned c) const
      { return m[r*C+c]; }
  private:
    double m[R*C];
};

template <unsigned R, unsigned C>
std::ostream& operator<<( std::ostream& str, const M<R,C>& m )
{
  for (unsigned r = 0; r < R; ++r) {
    str << "{" << m(r,0);
    for (unsigned c = 1; c < C; ++c)
      str << ", " << m(r,c);
    str << "}" << std::endl;
  }
  return str;
}

int main()
{
  double vals[4][4] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 0, 1, 2 },
                        { 3, 4, 5, 6 } };
  M<4,4> m( &(vals[0][0]) );
  std::cout << m << std::endl;
  return 0;
}


-- 
           Summary: optimization bug initializing from cast array
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: kraftche at cae dot wisc dot edu
 GCC build triplet: x86_64-unknown-linux-gnu
  GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44069



More information about the Gcc-bugs mailing list