c++:初始化指针队列时出现段错误

发布于 2024-12-06 19:43:34 字数 2389 浏览 0 评论 0原文

我正在尝试实现 CLRS 中描述的 BFS 算法。并具有以下内容:

#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <sstream>
using namespace std;
struct Node{
    char colour;
    int numNbr;
    Node* parent;
    int distance;
    int* neighbours;
    int* costs;
    int name;
    Node(int _numNbr,int _name){
        name = _name;
        colour = 'w';
        parent = 0;
        distance = -1;
        neighbours = new int[_numNbr];
        costs      = new int[_numNbr];
        numNbr = _numNbr;
    }
};

list<Node*> bfs(Node** &nodes,int numNodes,int startNode) {
    cout << "performing BFS\n";
    for(int i = 0; i < numNodes;i++) {
        nodes[i]->colour = 'w';
        nodes[i]->parent = 0;
    }
    cout << "All nodes painted white" <<endl;
    queue<Node*> q; // segfault occurs here
    cout << "initialised a queue" << endl;
    list<Node*> l;
    cout << "initialised a list" << endl;
    nodes[startNode]->colour = 'g';
    nodes[startNode]->distance = 0;
    q.push(nodes[startNode]);
    Node* u;
    Node* v;
    while(!q.empty()){
        u = q.front();
        for(int i = 0;i < u->numNbr; i++) {
            v = nodes[u->neighbours[i]];
            if(v->colour == 'w'){
                v->colour = 'g';
                v->distance = (u->distance)+1;
                v->parent = u;
                q.push(v);
            }
        }
        l.push_front(u);
        u->colour = 'b';
        q.pop();
    }
    return l;
}

int main(){
    int nodeCount;
    cin >> nodeCount;
    cin.ignore();
    Node** nodes = new Node*[nodeCount+1];
    for(int i = 0; i < nodeCount; i++){
        .... // build up the nodes in the adjacency list
    }
    list<Node*> l = bfs(nodes,nodeCount,1);
    cout << "BFS of nodes\n";
    for(list<Node*>::iterator it = l.begin();it != l.end();it++){
        cout << (*it)->name << " ";
    }
    cout << endl;
    return 0;
}

但是,当我运行此命令时,当队列初始化时,我只会得到以下输出,后跟一个段错误:

jonathan@debian:~/Code/cpp/dijkstra$ ./dijkstra 
3
1 2 1 3 1
2 3 1
3 1 1

performing bfs
All nodes painted white
Segmentation fault

我正在使用以下命令进行编译:

g++ -Wall  -o dijkstra dijkstra.cpp

I am trying to implement the BFS algorithm described in CLRS. And have the following:

#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <sstream>
using namespace std;
struct Node{
    char colour;
    int numNbr;
    Node* parent;
    int distance;
    int* neighbours;
    int* costs;
    int name;
    Node(int _numNbr,int _name){
        name = _name;
        colour = 'w';
        parent = 0;
        distance = -1;
        neighbours = new int[_numNbr];
        costs      = new int[_numNbr];
        numNbr = _numNbr;
    }
};

list<Node*> bfs(Node** &nodes,int numNodes,int startNode) {
    cout << "performing BFS\n";
    for(int i = 0; i < numNodes;i++) {
        nodes[i]->colour = 'w';
        nodes[i]->parent = 0;
    }
    cout << "All nodes painted white" <<endl;
    queue<Node*> q; // segfault occurs here
    cout << "initialised a queue" << endl;
    list<Node*> l;
    cout << "initialised a list" << endl;
    nodes[startNode]->colour = 'g';
    nodes[startNode]->distance = 0;
    q.push(nodes[startNode]);
    Node* u;
    Node* v;
    while(!q.empty()){
        u = q.front();
        for(int i = 0;i < u->numNbr; i++) {
            v = nodes[u->neighbours[i]];
            if(v->colour == 'w'){
                v->colour = 'g';
                v->distance = (u->distance)+1;
                v->parent = u;
                q.push(v);
            }
        }
        l.push_front(u);
        u->colour = 'b';
        q.pop();
    }
    return l;
}

int main(){
    int nodeCount;
    cin >> nodeCount;
    cin.ignore();
    Node** nodes = new Node*[nodeCount+1];
    for(int i = 0; i < nodeCount; i++){
        .... // build up the nodes in the adjacency list
    }
    list<Node*> l = bfs(nodes,nodeCount,1);
    cout << "BFS of nodes\n";
    for(list<Node*>::iterator it = l.begin();it != l.end();it++){
        cout << (*it)->name << " ";
    }
    cout << endl;
    return 0;
}

When I run this however, I only get the following output followed by a segfault when the queue is initialised:

jonathan@debian:~/Code/cpp/dijkstra$ ./dijkstra 
3
1 2 1 3 1
2 3 1
3 1 1

performing bfs
All nodes painted white
Segmentation fault

I am compiling with the following command:

g++ -Wall  -o dijkstra dijkstra.cpp

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

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

发布评论

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

评论(1

清旖 2024-12-13 19:43:34
list<Node*> bfs(...

当你返回时:

return l;

另外,这里不需要参考:

Node** &nodes

并且段错误没有发生在你指出的地方。因此,I/O 缓冲区未满,并且看起来

cout << "initialised a queue" << endl;
list<Node*> l;
cout << "initialised a list" << endl;

未执行

list<Node*> bfs(...

while you return:

return l;

also, no need for reference here:

Node** &nodes

And segfault didn't occur where you pointed. I/O buffers were not fulshed because of it and it loooks like this

cout << "initialised a queue" << endl;
list<Node*> l;
cout << "initialised a list" << endl;

wasn't executed

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