开发者

Segmentation fault in overloaded operator>>

I'm trying to write code for an orthogonal linked sparse matrix.

Here's the question:

An alternative linked representation for sparse matrices uses nodes that have the fields down, right, row, col, and value. Each non-zero entry of the sparse matrix is represented by a node. The zero terms are not explicitly stored. The nodes are linked together to form two circular lists. The rst list, the row list, is made up by linking nodes by rows and within rows by columns using the right field. The second,list, the column list, is made up by linking nodes via the down field. In this list, nodes are linked by columns and within columns by rows. These two lists share a common header node. In addition, a node is added to the dimensions of the matrix.

The input file looks like this:

// Matrix A

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 is my code for the 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;
    }

    for (int i = 2; i <= x.numTerms; i++) {

        in >> x.currentNode->row >> x.currentNode->col >> x.currentN开发者_运维问答ode->value;

        x.push_back(x.currentNode);

    }


    return in;

}

It compiles fine. But when I try running it, I keep getting a segmentation fault error.

Can anyone help?? Thanks a 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 for operator >> & <<, etc goes here, but everything's commented out except operator >>  

#endif

EDIT: I'm including operator<< as 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;

    for (int i = 0; i < x.numTerms; i++) {

        out << x.currentNode->row << x.currentNode->col << x.currentNode->value << endl;

    }

    return out;

}


I think your issue is with currentNode. You shouldn't need it as a class variable, and instead you should create a new one each time you read input from the stream. For example,

istream& operator>>(istream& in, OrthogonalLinkedSparseMatrix& x)
{
    in >> x.numRows >> x.numCols >> x.numTerms;

    int inRow, inCol, inValue;
    in >> inRow >> inCol >> inValue;         // Get the values from input

    // note: this allocates a NEW matrixNode on the heap, and pushes a pointer into the 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;
    }

    for (int i = 2; i <= x.numTerms; i++) 
    {
        in >> inRow >> inCol >> inValue;         // Get the values from input

        // note: this allocates a NEW matrixNode on the heap, and pushes a pointer into the matrix.
        x.push_back(new matrixNode(inRow, inCol, inValue));
    }

    return in;
}

A few things to note here:

  • Each time push_back() is called, a new matrixNode is pushed on. In your implementation, the same node was always added, and was probably never initialized.
  • I assumed that matrixNode has a constructor that takes 3 arguments. This should be easy to add.

In general, managing pointers yourself is very dangerous and prone to memory leaks. In this case, its important that you call delete on each pointer when you no longer need it. Most likely you'll want to do this in your destructor.

EDIT: It also looks like hnode may be being used with an invalid pointer. Make sure you assign/create this matrixNode pointer before using it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜