C++: copy dynamic array causes memory error in Linux -


i write 2 class vector , matrix, in these use copy copy dynamic array form 1 object copy contructor , operator =. if put seprately each code of these classes main, works fine, if put both of them, error occurs. here's code:

matrix.h

#include <iostream> #include <iomanip> #include <stdexcept>  using namespace std;  template <class u> class matrix {     int row, col;     u **data;      public:         matrix(int, int);         matrix(const matrix&);         matrix operator = (const matrix&);         ~matrix(); };  template <class u> matrix<u>::matrix(int row, int col) {     this->row = row;     this->col = col;     data = new u *[row];     (int = 0; < col; ++i)         data[i] = new u[col]; }  template <class u> matrix<u>::matrix(const matrix& mt) {     if (this != &mt) {         row = mt.row;         col = mt.col;         data = new u *[row];         (int = 0; < col; ++i)             data[i] = new u[col];         (int = 0; < row; i++)             copy(&mt.data[i][0], &mt.data[i][col+1], data[i]);             //memcpy(data[i], mt.data[i], col*sizeof(data[i]));     } }  template <class u> matrix<u>::~matrix() {     (int = 0; < row; i++) {         delete [] data[i];     }     delete [] data; }  template <class u> matrix<u> matrix<u>::operator = (const matrix& mt) {     if (row == mt.row , col == mt.col) {         (int = 0; < row; i++)             copy(&mt.data[i][0], &mt.data[i][col], data[i]);             //memcpy(data[i], mt.data[i], col*sizeof(data[i]));         return *this;     }     else throw runtime_error("matrices must have same size.");      return *this; } 

vector.h

#include <iostream> #include <stdlib.h> #include <stdexcept>  using namespace std;  template <class type> class vector {     int size;     type *data;      public:         vector(int);         vector(const vector&);         ~vector();         vector operator = (const vector&); };  template <class type> int vector<type>::get_size() {     return size; }  template <class type> vector<type>::vector(int size) {     if (size < 2) throw runtime_error("size must greater 1.");     else {         this->size = size;         data = new type[size];     } }  template <class type> vector<type>::vector(const vector& that) {     size = that.size;     data = new type[size];     copy(&that.data[0], &that.data[size+1], data);     //memcpy(data, that.data, size*sizeof(data)); }  template <class type> vector<type> vector<type>::operator = (const vector& that) {     cout<<"assigning..."<<endl;     if (size == that.size) {         copy(&that.data[0], &that.data[size], data);         //memcpy(data, that.data, size*sizeof(data));         return *this;     }     else throw runtime_error("vectors must have same size.");     return *this;     } 

main.cpp

#include <iostream> #include "vector.h" #include "matrix.h" #include <stdexcept>  using namespace std;  int main() {     try {         vector<int> a(3), c(3);         matrix<int> b(2,2), d(2,2);         // vector         cin>>a;         cout<<a;         c=a;         cout<<c;          //matrix         cin>>b;         d = b;         cout<<d;     }     catch (exception &e) {         cout<<e.what()<<endl;     } } 

and error:

vector_matrix_poly_demo: malloc.c:2372: sysmalloc: assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed. aborted (core dumped) 

a rest of code (overload operator >> , <<) didn't put in here 'cause works fine. error's in linux, use ubuntu 14.04 , if replace copy memcpy or block of matrix code go first, no error occurs.

best regards , sorry bad english.


Comments