方法调用时的c++段错误

标签 c++ pointers memory-management linked-list segmentation-fault

我正在处理作业,我必须在 C++ 中为给定的链表创建深拷贝构造函数。在复制构造函数中,代码和指针工作正常,但是当调用打印函数时,我在第 59 行的函数中遇到段错误:

cout << v->elem << " ";

我已经尝试调试了几个小时,但我不知道为什么会出现段错误。赋值的代码(只有复制构造函数中的代码是我的):

#include <iostream>
#include <stddef.h>
#include "intSLinkedList.h"

using namespace std;

intSLinkedList::intSLinkedList(const intSLinkedList& other){
    if(other.head == NULL){
        this->head = NULL;
    }

    else{
        intSNode* src_cursor = other.head;

        while(src_cursor != NULL){
            this->addFront(src_cursor->elem);
            src_cursor = src_cursor->next;  
        }
    }
}

intSLinkedList::intSLinkedList(): head(NULL), tail(NULL) { }

bool intSLinkedList::empty() const{ return head == NULL; }

const int& intSLinkedList::front() const{ return head->elem; }

//intSLinkedList::~intSLinkedList(){ while (!empty()) removeFront(); }

void intSLinkedList::addFront(const int& e) {// add to front of list
    intSNode* v = new intSNode;          // create new node
    v->elem = e;                         // store data
        v->next = head;                      // head now follows v
        head = v;                            // v is now the head
    if (head->next==NULL)
        tail = head;
}

void intSLinkedList::addBack(const int& e) {// add to front of list
        intSNode* v = new intSNode;          // create new node
        v->elem = e;
    v->next = NULL;                         // store data

    tail->next = v;                      // head now follows v
    tail = v;
}

void intSLinkedList::removeFront() {         // remove front item
        intSNode* old = head;                // save current head
        head = old->next;                    // skip over old head
        delete old;                          // delete the old head
}

void intSLinkedList::print() {
        intSNode* v = head;
        while (v != NULL){
            cout << v->elem << " ";
            v = v->next;
    }
    cout << endl;
}

int intSLinkedList::count() {
        intSNode* v = head;
    int n = 0;
        while (v != NULL){
        n++;
                v = v->next;
        }
    return n;
}

头文件:

class intSLinkedList;

class intSNode { 
private: 
    int elem; 
    intSNode* next;     
    friend class intSLinkedList; 
};

class intSLinkedList { 
public: 
    intSLinkedList();

    intSLinkedList(const intSLinkedList& other);

    bool empty() const;

    const int& front() const;

    //~intSLinkedList();

    void addFront(const int& e);

    void addBack(const int& e);

    void removeFront();

    void print();

    int count();

private: 
    intSNode* head; 
    intSNode* tail;
};

和测试文件:

#include <iostream>
#include "intSLinkedList.h"

using namespace std;

int main(){
    intSLinkedList int_sll;
    int_sll.addFront(5);
    int_sll.addFront(12);
    int_sll.addFront(6);
    cout << "int_sll : ";
    int_sll.print();

    intSLinkedList int_sll2 = int_sll;
    cout << "int_sll2 : ";
        int_sll2.print();

    int_sll.addBack(100);
    cout << "int_sll : ";
    int_sll.print();
    cout << "int_sll2 : ";
        int_sll2.print();

}

我的 GDB 输出:

int_sll : 6 12 5 

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400cce in intSLinkedList::print (this=0x7fffffffe010)
    at intSLinkedList.cpp:57
57              cout << v->elem << " ";

非常感谢任何帮助或指向正确方向的观点。

最佳答案

看起来你没有用任何东西初始化 next,所以它是一个未定义的值,你在导航到它时崩溃了。

您不会显示所有构造函数变体,因此请确保每个构造函数始终 初始化所有 属性。

关于方法调用时的c++段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46417579/

相关文章:

c++ - 准备好的语句可以跨线程共享吗?

C: 请帮我调试这个与内存相关的段错误

c - 在 C : swapping pointers leads to unexpected results 中排序

c - sizeof 运算符在 C 中接受哪些参数?

c - 结构和内存分配

c# - C# DLL 可以调用调用 native C++ 静态库的 C++/CLI 托管包装器吗?

java - Java 和 C++ 中的类似程序不同的输出

ios - ARC下不释放内存

c - 连续录制音频的内存问题

C++ - 如何将回调绑定(bind)到类方法而不是静态的?