【问题标题】:Implementing a Queue Class, Segmentation Fault?实现一个队列类,分段错误?
【发布时间】:2013-03-17 02:52:27
【问题描述】:

我正在尝试实现一个队列类(使用节点结构和队列类)。 我遇到了一个分段错误,而且我的眼睛都快瞎了,我似乎找不到它。 我的pushBack 不起作用,我很确定我的popFront 可能不起作用。我只是希望有人能够在正确的方向上给我一个很好的推动!

另外,如果你还没有弄清楚。我显然对 C++ 很陌生。

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* link;
};

class Queue {
public:
    Queue(); 
    ~Queue(); 
    void pushBack(int d);
    bool popFront(); 
    bool isEmpty(); 
    void displayQueue();
private:
    Node* back;
    Node* front;
};

Queue::Queue() {
    back = NULL;
    front = NULL;
}

Queue::~Queue() {
    while (!isEmpty()) {
        popFront();
    }
}

void Queue::pushBack(int d) {
    Node* temp;

    if (temp == NULL) {
        return;
    } else {
            temp->link = NULL;

            if (back == NULL) {
                  back = temp;
                  front = temp;
            } else {
                  front->link = temp;
              front = temp;
            }
      }
}


bool Queue::popFront() {
    if (front == NULL) {
        return false;
    } else {
        Node* removeNode;
        removeNode = front;

        if (back == front) {
            back = NULL;
            front = NULL;
        } else {
            Node* previousFront = back;
            while (previousFront->link != front) {
                previousFront = previousFront->link;
            }

            front = previousFront;
            front->link = NULL;
        }

        delete removeNode;
        return true;
    }
}

bool Queue::isEmpty() {
    return (back == NULL);    
}

void Queue::displayQueue() {
    if (isEmpty()) {
        cout << "Queue is empty!" << endl;
    } else {
        Node *current;

        current = back;

        cout << endl << "-- BACK --  ";

        while (current != NULL) {
        cout << current->data << "  ";
            current = current->link;
        }

        cout << "-- FRONT --" << endl << endl;
    }
}

int main(){
    Queue q;
    q.displayQueue();
    q.pushBack(20);
    q.pushBack(30);
    q.displayQueue();
    q.pushBack(40);
    q.pushBack(12);
    q.displayQueue();
    q.popFront();
    q.displayQueue();

    return 0;
}

【问题讨论】:

  • 使用 valgrind。它会告诉你代码哪里出错了。

标签: c++ segmentation-fault undefined-behavior


【解决方案1】:

至少有一个主要问题,在 pushBack 中,您使用 temp 而不对其进行初始化:

 void Queue::pushBack(int d) 
 {
   Node* temp;

    if (temp == NULL) {
        ^^^^

在打开警告的情况下编译会对您有所帮助,使用带有gcc-Wall 标志会给您以下警告:

warning: 'temp' is used uninitialized in this function [-Wuninitialized]
 if (temp == NULL) {
 ^

使用变量,像这样的未初始化的自动变量是undeined behavior,这意味着您的程序的行为是不可预测的。另请参阅Has C++ standard changed with respect to the use of indeterminate values and undefined behavior in C++1y? 以供参考。

您可能打算这样做:

   Node* temp = new Node();

   temp->data = d ;

虽然为Node 设置constructor 会更好。

【讨论】:

  • 谢谢!这正是我的问题所在。我会记住这一点,以备将来参考。 :)
【解决方案2】:

你不能设置这样的变量:temp-&gt;link = NULL;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 2017-01-08
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    相关资源
    最近更新 更多