c++ - Segmentation fault in overloaded operator>> -
i'm trying write code orthogonal linked sparse matrix.
here's question:
 alternative linked representation sparse matrices uses nodes have fields down, right, row, col, , value. each non-zero entry of sparse matrix represented node. 0 terms not explicitly stored. nodes linked form 2 circular lists. rst list, row list, made linking nodes rows , within rows columns using right field. second,list, column list, made linking nodes via down field. in list, nodes linked columns , within columns rows. these 2 lists share common header node. in addition, node added dimensions of matrix.  
the input file looks this:
// matrix
4   4   7
 1   1   2
 1   4   1
 2   2   7
 3   1   9
 3   3   8
 4   2   4
 4   3   5
// matrix b
4   4   5
 1   3   4
 2   1   6
 2   3   3
 3   2   5
 4   4   9    
this code operator>>:
istream& operator>>(istream& in, orthogonallinkedsparsematrix& x){      in >> x.numrows >> x.numcols >> x.numterms;     in >> x.currentnode->row >> x.currentnode->col >> x.currentnode->value;     x.push_back(x.currentnode);     if((x.currentnode->row == 1)&&(x.currentnode->col == 1)){          x.hnode->right = x.currentnode;         x.hnode->down = x.currentnode;     }     if(x.currentnode->col == 1){         x.hnode->down = x.currentnode;     }     if(x.currentnode->row == 1){         x.hnode->right = x.currentnode;     }      (int = 2; <= x.numterms; i++) {          in >> x.currentnode->row >> x.currentnode->col >> x.currentnode->value;          x.push_back(x.currentnode);      }       return in;  }   it compiles fine. when try running it, keep getting segmentation fault error.
 can help?? bunch!  
here's orthogonallinkedsparsematrix.h:
#ifndef o_l_sparse_matrix_h  #define o_l_sparse_matrix_h    #include <iostream>  #include <fstream>  #include "node.h"  #include "myexceptions.h"    using namespace std;    class orthogonallinkedsparsematrix;  ostream& operator<< (ostream&, orthogonallinkedsparsematrix&);  istream& operator>> (istream&, orthogonallinkedsparsematrix&);    class orthogonallinkedsparsematrix{  public:      friend ostream& operator<<(ostream& out, orthogonallinkedsparsematrix& x);      friend istream& operator>>(istream& in, orthogonallinkedsparsematrix& x);      orthogonallinkedsparsematrix(){}      ~orthogonallinkedsparsematrix(){}      void transpose(orthogonallinkedsparsematrix &b);      void add(orthogonallinkedsparsematrix &a, orthogonallinkedsparsematrix &c);      void push_back(matrixnode *&mat_node);      void setdowns(matrixnode *&mat_node);  private:      matrixnode *hnode;      int numrows, numcols, numterms;      matrixnode *currentnode;      matrixnode *previousnode;      matrixnode *nextnode;  };   // code operator >> & <<, etc goes here, everything's commented out except operator >>    #endif   edit: i'm including operator<< well:
    ostream& operator<<(ostream& out, orthogonallinkedsparsematrix& x){      if(x.numterms == 0){          out << "no non-zero terms" << endl;          return out;      }      out << x.numrows << x.numcols << x.numterms << endl;      (int = 0; < x.numterms; i++) {          out << x.currentnode->row << x.currentnode->col << x.currentnode->value << endl;      }      return out;  }      
i think issue currentnode.  shouldn't need class variable, , instead should create new 1 each time read input stream.  example,
istream& operator>>(istream& in, orthogonallinkedsparsematrix& x) {     in >> x.numrows >> x.numcols >> x.numterms;      int inrow, incol, invalue;     in >> inrow >> incol >> invalue;         // values input      // note: allocates new matrixnode on heap, , pushes pointer matrix.     x.push_back(new matrixnode(inrow, incol, invalue));      if(x.currentnode->col == 1){         x.hnode->down = x.currentnode;     }     if(x.currentnode->row == 1){         x.hnode->right = x.currentnode;     }      (int = 2; <= x.numterms; i++)      {         in >> inrow >> incol >> invalue;         // values input          // note: allocates new matrixnode on heap, , pushes pointer matrix.         x.push_back(new matrixnode(inrow, incol, invalue));     }      return in; }   a few things note here:
- each time 
push_back()called, new matrixnode pushed on. in implementation, same node added, , never initialized. - i assumed matrixnode has constructor takes 3 arguments. should easy add.
 
in general, managing pointers dangerous , prone memory leaks.  in case, important call delete on each pointer when no longer need it.  you'll want in destructor.
edit: looks hnode may being used invalid pointer.  make sure assign/create matrixnode pointer before using it.
Comments
Post a Comment