https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62003

            Bug ID: 62003
           Summary: Possible bug in std::complex
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wilczak at ii dot uj.edu.pl

Dear MinGW users,
I have found a strange behaviour of std::complex class with double template
parameter. The program given below causes segfault (gcc-4.8.1-4, Windows) when
compiled with -O1 or -O2 flag. 

The output of the program is given below (note that the second column equal to
e=v.end() should be constant).

No errors when:
- typedef Complex=double
- parameter -O0 is used
- volatile added to definition of iterator 'e' 
volatile Vector::iterator e = u.end();
- type of variable 'p' is changed to double instead of Complex 

Daniel Wilczak

// ###########################
// Output

0x3f19b0 0x3f1a40 (0,0)
0x3f19c0 0x3f1a40 (0,0)
0x3f19d0 0x3f1a40 (0,0)
0x3f19e0 0 (0,0)
0x3f19f0 0 (0,0)
0x3f1a00 0x40000000 (0,0)
0x3f1a10 0 (0,0)
0x3f1a20 0 (0,0)
0x3f1a30 0x1 (0,0)
0x3f1a40 0x405064 (2.06124e-309,3.4598e-307)
0x3f1a50 0x6fcc43c0 (0,4.24399e-314)
0x3f1a60 0x4038dd (2.04322e-317,3.711e+261)
0x3f1a70 0x28fee8 (1.73018e-307,2.47437e+261)

// ###########################
// The program 

#include <iostream>
#include <complex>

template<class T>
struct V{
  typedef T* iterator;
  iterator begin() { return data; }
  iterator end() { return data+capacity; }
  V(unsigned c) : capacity(c) { data = new T[c]; }
  ~V(){ delete[] data; }
  T *data;
  unsigned capacity;
};

int main()
{
  typedef std::complex<double> Complex;
  typedef V<Complex> Vector;
  Vector u(9);
  Vector::iterator j = u.begin();
  Vector::iterator e = u.end();
  for(; j!=e; ++j) {
    Complex p(2.,0.);
    Complex q = *j;
    Complex z = q*p;
    std::cout << j << " " << e << " " << z << std::endl;
  }
  return 0;
}

Reply via email to