重载运算符中的分段错误>>

发布于 2024-10-19 14:37:48 字数 3252 浏览 2 评论 0原文

我正在尝试为正交链接稀疏矩阵编写代码。

问题如下:
稀疏矩阵的另一种链接表示使用具有向下、向右、行、列和值字段的节点。表示稀疏矩阵的每个非零条目 通过一个节点。零项没有显式存储。节点链接在一起形成两个循环列表。第一个列表,即行列表,是通过使用右侧字段按行链接节点以及按列链接行内节点组成的。第二个列表,即列列表,是通过 down 字段链接节点组成的。在此列表中,节点按列链接,列内按行链接。这两个列表共享一个公共头节点。此外,还向矩阵的维度添加了一个节点。

输入文件如下所示:

// 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

// 矩阵 B

4 4 5
1 3 4
2 1 6
2 3 3
3 2 5
4 4 9

这是我的运算符>>的代码:

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.currentNode->value;

        x.push_back(x.currentNode);

    }


    return in;

}

它编译得很好。但是当我尝试运行它时,我不断收到分段错误错误。
有人可以帮忙吗? 非常感谢!

这是 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

编辑:我包括运算符<<还有:

    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'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.currentNode->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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屌丝范 2024-10-26 14:37:48

我认为您的问题出在 currentNode 上。您不应该需要它作为类变量,而是应该在每次从流中读取输入时创建一个新变量。例如,

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;
}

这里需要注意一些事项:

  • 每次调用push_back()时,都会推送一个新的matrixNode。在您的实现中,始终添加相同的节点,并且可能从未初始化。
  • 我假设 MatrixNode 有一个带有 3 个参数的构造函数。这应该很容易添加。

一般来说,自己管理指针是非常危险的,并且容易出现内存泄漏。在这种情况下,当您不再需要每个指针时,对每个指针调用 delete 非常重要。您很可能希望在析构函数中执行此操作。

编辑:
看起来 hnode 可能正在与无效指针一起使用。确保在使用它之前分配/创建这个矩阵节点指针。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文