In the course of my programming I have created a new class called Vector (yes I know that vector.h exists, but I like my version better, or at least I have until now). As part of my class, I have overloaded the = to use as an assingment function, as in A=B.
Here's the code:

Vector& Vector:: operator=(const Vector& u)
   {
    if (this!=&u) // check for self assignment
      {
       delete[] v;
       double *newdata=new double[u.L];
       for (int i=0; i<u.L; i++) {newdata[i]=u(i);}
       v = newdata;
       L = u.L;
      }
    return *this;
   }

By why of explanation, u.L returns the length of the Vector. v is the hidden array within the Vector that holds its contents. The element of a Vector is accessed using Vector(element number).

The interesting thing is, this code compiles and runs just fine with the Borland C++ compiler, and I have been using this code for years without problems. I recently updated to the MinGW compiler. The code still compiles. However, when I run the program this assignment function causes the program to crash, with the console error message:

Process returned -1073741795 (0xC000001D) execution time : 3.260 s

Now, the really weird part is that in tracking down this error I added an output to the function as follows:

for (int i=0; i<u.L; i++) {newdata[i]=u(i); cout << newdata[i];}

When I add this, the function and the program compiles and runs just fine.

Now, the question is: WHY?

What have I done wrong?
What am I missing?
Why does the function work properly with the useless cout << newdata[i]; added, but crashes without it?
Why does it work as expected in the Borland C++ compiler but not in MinGW?

I suppose the answer to my last question is that that there is something new to the more modern MinGW compiler, but I do not know what that might be. Still, isn't code using older conventions (if that is what I am doing) still supposed to work properly?

Further information.

I am now pretty certain that the problem lies with newdata[i]=u(i);

I can get the program to output Vector(i) values and array[i] values, but it crashes at the point where I try to get array[i]=Vector(i). So, I am thinking the problem might lie somewhere in my class definition. So, here is the entire Vector Class. . .

class Vector
 {
  private:
    double *v; //data
    int L;
  public:
   Vector (): v(new double[1]), L(1){ };                // default constructor
   Vector (int n): v(new double[n]), L(n) { }           // constructor
   ~Vector() {delete[] v; };                            // destructor
   double& operator() (const int n) {return v[n];}      // output a value from [cell]
   double& operator() (const int n) const  {return v[n];}
   Vector(const Vector& u): v(new double[u.L]), L(u.L)  //copy constructor
       {for (int i=0; i<L; i++) v[i]=u(i); }

   int length () const {return L;};
   void set (const int n) {L=n; v=new double[n];}

   void dump(fstream &fout) const;
   void dump() const;
   void dump(const int P, const int W) const;

   Vector& operator=(const Vector& u);
   Vector operator+ (const Vector&);
   Vector operator+ (const double&);
   Vector operator- (const Vector&);
   Vector operator- (const double&);
   Vector operator* (const double&);
   Vector operator/ (const double&);
   Vector operator* (Vector& u); // multiply two quaternion vectors
   bool operator== (const Vector a);
   bool operator!= (const Vector a);
   Vector crosswith(const Vector &);
   double dot (const Vector&);


   double magnitude();
   Vector conjugate();
   Vector inverse();
   Vector MakeUnitVector ();
   double DistanceFrom (const Vector p);
//   bool EqualTo(const Vector a);
   Matrix MatrixFrom(const double);
   Matrix toMatrix();
   Matrix toMatrix(const string type);
   Matrix make4x4(const string type);
   bool isUnit() {return (fabs(magnitude()-1)<precision);};
   bool isMissing (); // returns true if any cell has the missing value
   char referent();
   Matrix translationMatrix(); //returns a 4x4 matrix 0's with 1's on diagonal and the vector as the 4th column
   Vector reduce(const int e); // reduces the length of a vector to e elements long
   bool isAttitude();
   double min();
   double max();
   double range();
 };

Once again, this compiles just fine. It runs when compiled with Borland, but not with minGW.

OK, still more information.

It would seem that the problem is more complicated and related to the MinGW compiler. I resurrected an old (two day old) version of the code and it compiled and ran just fine.

What did I do?

I added two new functions. One copies a Vector into an array. The other copies an array into a Vector. Neither are super complicated functions. And neither of them are being called when I invoke a Vector assignment, such as A=B;
Nonetheless, this change causes the program to crash.

So, I delete the two new functions completely (essentially restoring my code to the older version) and recompile. It compiles. BUT, it still does not run. The program comes to the point where A=B and crashes. Crashing where it did not crash before.

So, why?

I could accept some weird memory leak is going on, except that I cannot even restore the code to its original version and get it to run. I have to recopy the older code to get things to compile and run.

AND, it is not just these two new functions. ANY change to the code, such as fixing a spelling error in an output string or adding a /comment/ will cause the program to crash when it reaches A=B.

If it matters, I use Code::Blocks as my IDE.

I'm up for any ideas or suggestions.

This article has been dead for over six months. Start a new discussion instead.