class - c++ segmentation fault when deleting dynamic tables -


everyone have make dynamic matrix , here constructors , destructor have:

board::board() {     a_l=0;     a_c=0;     a_mp=null; } board::board(const board&t) {     a_l=t.a_l;     a_c=t.a_c;     a_mp=t.a_mp;     memory();     copy(t); } board::board(int nl, int nc) {     a_l=nl;     a_c=nc;     memory(); } board::~board() {     freememory(); }  // private methods  void board::copy(const board &t) {     int a_l, a_c;     int ** a_mp;     a_l=t.a_l;     a_c=t.a_c;     for(int i=a_l;i<a_c;i++) {         for(int j=a_c;j<a_l;j++) {             a_mp[i][j]=t.a_mp[i][j];         }     } } void board::freememory() {     for(int i=0;i<a_l-1;i++) {         delete [] a_mp[i];     }     delete [] a_mp; } void board::memory() {     char ** a_mp;     a_mp = new char*[a_l];     for(int =0;i<a_l; i++) {         a_mp[i]=new char[a_c];         for(int j=0;j<a_c;j++)             a_mp[i][j]='-';     } } 

board class , a_l , a_c number of lines , columns of matrix. in main, declare board variable , this:

board=board(5,5); 

it compiles, when want display it, example:

cout << board.cols() << endl; 

this method:

int board::cols() const {     return (a_c); } 

it displays 0. if didn't create board parameters said. program crashes when board=board(5,5); use debugger , says stops @ line of delete:

board=board(5,5); 

i don't know why crashes , don't know why doesn't keep values of board variable i've declared! knows why?

edit: rmemory=memory, type here not program

firstly, assignment

board=board(5,5); 

is over-complicated. can declare

board board(5,5); 

directly (the work may optimized out, it's un-idiomatic).


secondly, everywhere like:

void board::copy(const board &t) {     int a_l, a_c;     int ** a_mp; 

you're shadowing object's member variables. is, a_l refers local integer variable inside function, instead of object member. you'll have refer this->a_l if want see or change object.


now, copy constructor

board::board(const board&t) {     a_l=t.a_l;     a_c=t.a_c;     a_mp=t.a_mp;     memory();     copy(t); } 

does:

  1. a shallow copy (it shares a_mp pointer object you're copying)
  2. calls memory allocates new lump of memory, referred a_mp inside memory, , leaked (when local variable goes out of scope)
  3. calls copy, copies original objects values uninitialized local pointer called a_mp (this undefined behaviour, because writes go anywhere)
  4. ends value of a_mp first chose, two board instances have same a_mp value, , freed twice.

here's sample code fixes problems: i've changed structure of code little possible (bar renaming things readability). there's plenty of room improvement, should @ least correct.

class board {     int rows;     int cols;     char **data; public:     board(): rows(0), cols(0), data(0) {}     board(int nl, int nc) : rows(nl), cols(nc)      {         allocate_data();     }     board(const board& other)         : rows(other.rows), cols(other.cols)     {         allocate_data();         copy_data(other);     }     ~board() {         free_data();     } private:     void copy_data(const board &other) {         for(int r=0; r<rows; r++)             for(int c=0; c<cols; c++)                 data[r][c]=t.data[r][c];     }     void free_data() {         for(int r=0; r<rows; r++)             delete [] data[r];         delete [] data;     }     void allocate_data() {         data = new char*[rows];         for(int r=0; r<rows; r++) {             data[r]=new char[cols];             for(int c=0; c<cols; c++)                 data[r][c]='-';         }     } }; 

note works fine if only use copy constructor, default-generated assignment operator still wrong. daniel weber pointed out in comment, rule of three suggests ought write well:

    board& operator=(const board& other) {         free_data();         rows = other.rows;         cols = other.cols;         allocate_data();         copy_data(other);     } 

note copy assignment operator needs cope destination object initialized, , may not have right dimensions. improve re-allocate if new (other) board larger.

if have c++11 support, can add move equivalents of copy constructor , assignment operator:

    board(board&& original)         : rows(original.rows), cols(original.cols)     {         data = original.data;         original.data = null;     }     board& operator=(board&& original) {         free_data();         rows = original.rows;         cols = original.cols;         data = original.data;         original.data = null;     } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -