C++ Error with big three leak in memory-pointer being freed was not allocated (not duplicate) -


my code not run correctly , don't know how fix it. not duplicate question asking rule of 3 because post not me in solving question in post im using pointer pointer array. don't know did wrong in big 3 functions can please me correct mistake. compiler highlighting delete[] matrix[i]; in the destructor when i=2 inside loop

in header file have:

#ifndef fasdf_dynn_h #define fasdf_dynn_h  #include <iostream> #include <fstream> #include<string> #include <cstdlib> #include <vector>  using namespace std; template <class t>  class matrixdynvector{  public:    // matrixdynvector()       {         //creates 3 3 matrix elements equal 0         m=3;         n=3;          matrix=new int*[m];        for(int i=0;i<m;i++)          matrix[i]=new int[n];      for(int i=0;i<m;i++)         for(int j=0;j<n;j++)            matrix[i][j]=0;       }    // matrixdynvector(int m,int n);      template <class h>     matrixdynvector<h>(const matrixdynvector<h>& c)//copy constructor     {         m=c.m;         n=c.n;          (int = 0; < c.m; i++)             (int j = 0; j < c.n; j++)                 matrix[i][j] = c.matrix[i][j]; // add data     }      template <class h>     matrixdynvector<h>& operator =(const matrixdynvector<h>& c)//asignment     {         if (this == &c)         {             return *this;         }         else         {             matrix = new int*[c.m];             (int = 0; < c.m; i++)                 matrix[i] = new int[c.n]; // create multi dimensional array              (int = 0; < c.m; i++)                 (int j = 0; j < c.n; j++)                     matrix[i][j] = c.matrix[i][j]; // add data              (int = 0; < c.m; i++)                 delete[] matrix[i]; // delete second dimension of matrix              delete[] matrix; // delete first*/              return *this;         }     }      ~matrixdynvector()     {         if(matrix!=null)         {             (int = 0; < m; i++)                delete[] matrix[i]; // delete second dimension of matrix              delete[] matrix; // delete first              matrix=null;         }     } private:     int m,n;     int** matrix; };   #endif 

i can't compile verify think should have that:

edit: after compile , chatting question's author. here isa working version.

#ifndef fasdf_dynn_h #define fasdf_dynn_h  #include <iostream> #include <fstream> #include <string> #include <cstdlib>  using namespace std;  template <class t> class matrixdynvector { private:     int m,n;     t **matrix;  public:     matrixdynvector();     matrixdynvector(int m,int n);     matrixdynvector(t diagonal, int n, int m);     matrixdynvector(const matrixdynvector<t>& c);//copy constructor     virtual ~matrixdynvector();      matrixdynvector<t>& operator =(const matrixdynvector<t>& c); //assignment     matrixdynvector<t> operator -();      friend ostream& operator <<(ostream& outs, const matrixdynvector<t> &obj) {         (int = 0; < obj.m; i++) {                 (int j = 0; j < obj.n; j++) {                     outs << " "<< obj.matrix[i][j];                 }                 outs<<endl;             }             outs<<endl;              return outs;     }     friend istream& operator >>(istream& in, matrixdynvector<t> &obj) {         (int = 0; < obj.m; i++) {             (int j = 0; j < obj.n; j++) {                 //if (in==cin)                     //cout <<"input elements element @ row:"<<i<<" column:"<<j<<": ";                 in >> obj.matrix[i][j];             }         }          return in;     }      friend matrixdynvector<t> operator *(const matrixdynvector<t>& a, const matrixdynvector<t>& b) {         matrixdynvector<t> product(a.m, a.n);              (int i=0;i<a.m; i++) {                 (int j=0;j<a.n; j++) {                     t sum=0;                     (int k=0; k<a.n; k++) {                         sum = sum+a.matrix[i][k]*b.matrix[k][j];                     }                     product.matrix[i][j]=sum;                 }             }              return product;     }     friend matrixdynvector<t> operator +(const matrixdynvector<t>& a, const matrixdynvector<t>& b) {         matrixdynvector<t> additon(a.m, a.n);   // should initialize size.              if((a.n != b.n) || (a.m != b.m)) {                 // here should throw error : 2 matrixes should same size.             }              for(int i=0;i<a.m;i++) {                 for(int j=0;j<b.n;j++) {                     additon.matrix[i][j] = a.matrix[i][j] + b.matrix[i][j];                 }             }              return additon;     }     friend matrixdynvector<t> operator -(const matrixdynvector<t>& a, const matrixdynvector<t>& b) {         matrixdynvector<t> subtract(a.m, a.n);          if( a.m!= b.m || a.n!= b.n) {//if have different rows , columns cant equal             // throw exception.         }         else {             for(int i=0;i<a.m;i++) {                 for(int j=0;j<b.n;j++) {                     subtract.matrix[i][j]=a.matrix[i][j]-b.matrix[i][j];                 }             }         }          return subtract;      }     friend bool operator ==(const matrixdynvector<t>& a,const matrixdynvector<t>& b) {         //if have different rows , columns cant equal             if( a.m!= b.m || a.n!= b.n) {                 return false;             }             else {                 for(int i=0;i<a.m; i++) {                     for(int j=0;j<b.n; j++) {                         if(a.matrix[i][j] != b.matrix[i][j]) {                             cout<<a.matrix[i][j]<< " not equal "<<b.matrix[i][j]<<endl;                             return false;                         }                     }                 }                  return true;             }     } };   template <class t> matrixdynvector<t>::matrixdynvector() {     m=3;     n=3;      matrix=new t*[m];      for(int i=0; i<m; i++) {         matrix[i]=new t[n];         for(int j=0;j<n;j++) {             matrix[i][j]=0; // careful! if t not int, 0 can bad initializer !!!         }     } }  template <class t> matrixdynvector<t>::matrixdynvector(int x,int y) {     m = x;     n = y;      matrix=new t*[m];      for(int i=0; i<m; i++) {         matrix[i]=new t[n];         for(int j=0;j<n;j++) {             matrix[i][j]=0; // careful! if t not int, 0 can bad initializer !!!         }     } }  template <class t> matrixdynvector<t>::matrixdynvector(t diagonal, int x, int y) {     m=x;     n=y;      matrix=new t*[m];      for(int i=0;i<m;i++) {         matrix[i]=new t[n];         for(int j=0;j<n;j++) {             if(i==j) {                 matrix[i][j]=diagonal;             }             else {                 matrix[i][j]=0;// careful! if t not int, 0 can bad initializer !!!             }         }     } }  template <class t> matrixdynvector<t>::matrixdynvector(const matrixdynvector<t>& c) {     m=c.m;     n=c.n;      matrix = new t*[m];      (int = 0; < m; i++) {         matrix[i] = new t[n];         (int j = 0; j < c.n; j++) {             matrix[i][j] = c.matrix[i][j]; // add data         }     } }  template <class t> matrixdynvector<t>::~matrixdynvector() {     if(matrix!=null) {         (int = 0; < m; i++) {             delete[] matrix[i]; // delete second dimension of matrix         }          delete[] matrix; // delete first          matrix=null;     } }  template <class t> matrixdynvector<t>& matrixdynvector<t>::operator =(const matrixdynvector<t>& c) {     if(this != &c) {         if(matrix != null) {             (int = 0; < m; i++) {                 delete[] matrix[i]; // delete second dimension of matrix             }             delete[] matrix; // delete first*/         }          m=c.m;         n=c.n;          matrix = new t*[c.m];          (int = 0; < c.m; i++) {             matrix[i] = new t[c.n]; // create multi dimensional array             (int j = 0; j < c.n; j++) {                 matrix[i][j] = c.matrix[i][j]; // add data             }         }     }      return *this; }  template <class t> matrixdynvector<t> matrixdynvector<t>::operator -() {     matrixdynvector<t> new_matrix(m, n);      for(int i=0;i<m;i++) {         for(int j=0;j<n;j++) {             new_matrix.matrix[i][j]=-matrix[i][j];         }     }     return new_matrix; }  #endif 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -