title: Segmentation fault overloading ostream (<<)
body: <p>So I'm practicing coding in c++ and I'm attempting to write a class for matrices (stored as arrays) with the relevant overloaded operations.</p>
  
  <p>I've got as far as defining the class and attempting to overload the &lt;&lt; operator but my current code is causing a segmentation fault (I compile with g++ in Ubuntu). I've looked online and people with similar problems tend to always forget to return os in their overload function, but I've done that so I have no idea what my problem could be. Furthermore, my overloaded operator works several times before it causes a segmentation fault.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;stdlib.h&gt; // for c style exit
  using namespace std;
  
  class matrix
  {
    // Friends
    friend ostream &amp; operator&lt;&lt;(ostream &amp;os, const matrix &amp;mat);
    friend istream &amp; operator&gt;&gt;(istream &amp;is, matrix &amp;mat);
  
  private:
    double *mdata;
    int rows,columns;
  public:
    // Default constructor
    matrix(){mdata=0; rows=columns=0;}
    // Parameterized constructor
    matrix(int m, int n){mdata = new double[ m*n ]; rows = m; columns = n;}
    // Copy constructor
    matrix(matrix &amp;mat)
    // Destructor
    ~matrix(){delete[] mdata; cout&lt;&lt;"Destructing array."&lt;&lt;endl;}
    // Access functions
    int getrows() const {return rows;} // Return number of rows
    int getcols() const {return columns;} // Return number of columns
    int index(int m, int n) const // Return position in array of element (m,n)
    {
      if(m&gt;0 &amp;&amp; m&lt;=rows &amp;&amp; n&gt;0 &amp;&amp; n&lt;=columns) return (n-1)+(m-1)*columns;
      else {cout&lt;&lt;"Error: out of range"&lt;&lt;endl; exit(1);}
    }
    double &amp; operator()(int m, int n)const {return mdata[index(m,n)];}
    // Other access functions go here
    double &amp; operator[](int i) {return mdata[i];}
    // Other functions 
    // Copy  Assignment operator
    matrix &amp; operator=(matrix &amp;mat);
  };
  
  // Member functions defined outside class
  matrix::matrix(matrix &amp;mat){
    rows = mat.getrows();
    columns = mat.getcols();
    for(int j = 0; j&lt;rows*columns; j++){mdata[j] = mat[j];}
   }
  
  matrix &amp; matrix::operator=(matrix &amp;mat){
    if (&amp;mat == this) return *this;
  
    delete[] mdata; rows = 0; columns = 0;
  
    rows = mat.getrows(); columns = mat.getcols();
    if(rows&gt;0&amp;&amp;columns&gt;0){
      mdata = new double[(columns-1) + (rows-1)*columns + 1];
      for(int j = 0; j&lt;rows*columns; j++){mdata[j] = mat[j];}
    }
    return *this;
  }
  
  
  // Overload insertion to output stream for matrices
  ostream &amp; operator&lt;&lt;(ostream &amp;os, const matrix &amp;mat){
    for(int j = 0;j&lt;mat.rows;j++){
      for(int k = 0;k&lt;mat.columns;k++){
        os &lt;&lt; mat(j+1,k+1) &lt;&lt; " ";
     }
      os &lt;&lt; endl;
   }
    return os;
  }
  
  // Main program
  
  int main(){
  
    // Demonstrate default constructor
    matrix a1;
    cout&lt;&lt;a1;
  
    // Parameterized constructor
    const int m(2),n(2);
    matrix a2(m,n);
    // Set values for a2 here
    a2[0] = 1; a2[1] = 2; a2[2] = 3; a2[3] = 4;
    // Print matrix a2
    cout&lt;&lt;a2;
  
  
    // Deep copy by assignment: define new matrix a3 then copy from a2 to a3
    matrix a3(m,n);
    cout&lt;&lt;a3;
    a3=a2;
    cout&lt;&lt;a3;
    // Modify contents of original matrix and show assigned matrix is unchanged here
    a2[0] = 5;
    cout&lt;&lt;a2;
    cout&lt;&lt;a3; //here is where segmentation fault occurs
    return 0;
  }
  </code></pre>