C++ heap corruption detected occurs while I try to use the function I made in derived class
<p>Here comes my code. Firstly I have defined the three class</p>

<p><code>class matrix {protected:
        double *mdata;
        int rows,columns;</code> </p>

<p><code>class polyg{protected: matrix x,y,z,centre;</code></p>

<p><code>class triangle: public polyg{protected:public:
    triangle(matrix x1,matrix y1,matrix z1){x=x1;y=y1;z=z1;
    centre=(x+y+z).change_scale(1/3);
    }</code></p>

<p>and I have defined a overloading function for matrix multiplication such as </p>

<pre><code>matrix operator*(matrix &amp;m) const{
  if(columns!=m.getrows()) {
      cout&lt;&lt;"invalid size!!"&lt;&lt;endl; exit(1);
  }
  matrix temp(rows,m.getcols());
  for(int x=0; x&lt;rows; x++) {
      for(int y=0; y&lt;m.getcols(); y++) {
          double value = 0;
          for(int z=0; z&lt;columns; z++) {
              value = value+(mdata[index(x+1,z+1)]*m.mdata[index(z+1,y+1)]);
          }
          temp.mdata[index(x+1,y+1)] = value;
      } 
  }
  return temp;}
</code></pre>

<p>index function is defined as</p>

<pre><code>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);}
</code></pre>

<p>}</p>

<p>and I defined member function, which overrides the pure virtual function in polyg class, in triangle class such as</p>

<pre><code>    void rotate(double angle){

double pi=4*atan(1.0);
double angle_rad=pi*angle/180;

matrix m_rot(2,2);
m_rot(1,1)=cos(angle_rad);
m_rot(1,2)=sin(angle_rad);
m_rot(2,2)=cos(angle_rad);
m_rot(2,1)=-sin(angle_rad);//matrix of rotation of angle inserted

x=m_rot*x;
y=m_rot*y;
z=m_rot*z;//rotating the triangle

}
</code></pre>

<p>problem occurs from</p>

<pre><code>x=m_rot*x;
y=m_rot*y;
z=m_rot*z;
</code></pre>

<p>this part. Heap corruption detected error occurs from this part. if I remove this part, code runs totally well without any problem.</p>

<p>Also, if I define in main </p>

<pre><code>int main(){matrix a,b,c; c=a*b;
</code></pre>

<p>it works totally well also.</p>

<p>however if I use the function I made in triangle class,for example,</p>

<pre><code>triangle tri(a,b,c); tri.rotate(30);
</code></pre>

<p>problem occurs</p>

<p>no errors occur before the debugging 
but after I compile,
Heap Corruption detected error occurs</p>

<p>could someone explain what is the problem? and how to fix it?</p>
