c++ - C++ : Attempting to add new node to linked list yields “Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)” error

标签 c++ xcode error-handling linked-list exc-bad-access

我正在编写一个用于作业的程序,该程序可以创建和处理链接列表。我在Node.cpp的Node::SetData函数以及List.cpp的List::Add_End行(特别是“current-> SetData(data);”)中遇到一行“EXC_BAD_ACCESS”错误,并且由于某种原因,main.cpp中的行(特别是“//Add_End节点到列表”)。我假设一旦修复了Node::SetData错误,这些其他错误将自行解决。

在搜索Stack Overflow和Google之后,我无法确定为什么会发生此错误。我以为这个问题(New to C++, "EXC_BAD_ACCESS" error I don't understand)会有所帮助,但是我仍然遇到问题。

我犯了什么编码错误?

main.cpp

#include <iostream>
#include <cstddef>
using namespace std;

#include "List.h"

int main()
{
    // New list
    List list;
    Node *answer;

    // Add_End nodes to the list
    list.Add_End(111);
    list.Print();
    list.Add_End(222);
    list.Print();
    list.Add_End(333);
    list.Print();
    list.Add_End(444);
    list.Print();
    list.Add_End(555);
    list.Print();

    // Delete nodes from the list
    list.Delete(444);
    list.Print();
    list.Delete(333);
    list.Print();
    list.Delete(222);
    list.Print();
    list.Delete(555);
    list.Print();
    list.Delete(111);
    list.Print();

    cout << "Testing Add_Front: and others" << endl;
    list.Add_Front(888);
    list.Print();
    list.Add_Front(999);
    list.Print();
    list.Add_Front(49);
    list.Print();

    cout << "Checking find function" << endl;
    answer = list.Find(888);
    cout << "Value for node returned by find function call with 888 is " << answer->Data() << "." << endl;
    cout << "Checking find function" << endl;
    answer = list.Find(999);
    cout << "Value for node returned by find function call with 888 is " << answer->Data() << "." << endl;
    cout << "Checking find function" << endl;
    answer = list.Find(49);
    cout << "Value for node returned by find function call with 888 is " << answer->Data() << "." << endl;
    cout << "Call find function with value not in list." << endl;
    answer = list.Find(7);
    if (answer == NULL)
    {
        cout << "returned null pointer since 7 not found" << endl;
    }
    else
    {
        cout << "in else of answer == NULL where Value for node returned by find function call with 7 is " << answer->Data() << "." << endl;
    }

    cout << "testing delete_front: " << endl;
    list.Delete_Front();
    list.Print();
    cout << "testing delete_end: " << endl;

    list.Delete_End();
    list.Print();

    return 0;
}

list .h
#ifndef LIST_H
#define LIST_H

#include <cstddef>

#include "Node.h"

class List
{
private:
    Node* head;

public:
    List();
    void Add_End(int data);
    void Delete(int data);
    void Delete_Front();
    void Add_Front(int data);
    void Delete_End();
    Node* Find(int data);
    void Print();    
};

#endif

List.cpp
#include <iostream>
#include <cstddef>
using namespace std;

#include "List.h"

List::List()
{
    head = NULL;
    return;
}

void List::Add_End(int data)
{
    Node* current;
    Node* newEnd = new Node();

    for (current = head; current != NULL; current = current->Next())
    {}
    current->SetData(data);
    current->SetNext(newEnd);
    newEnd->SetData(NULL);
    newEnd->SetNext(NULL);

    return;
}

void List::Delete(int data) {
    /*
     FILL IN CODE (will do later)
     */


    return;
}

void List::Delete_Front()
{
    /*
     FILL IN CODE (will do later)
     */

    return;
}

void List::Add_Front(int data)
{
    Node* newNode = new Node();
    newNode->SetData(data);
    newNode->SetNext(head);
    head = newNode;
    return;
}

void List::Delete_End()
{
    if (head == NULL)
    {
        cout << "List has no member so cannot delete end" << endl;
        return;
    }

    // check if one in length
    if (head->Next() == NULL)
    {
        head = NULL;
        return;
    }
    // 2 or greater in length

    Node* current;
    Node* prev;
    prev = head;
    for (current = head->Next(); current->Next() != NULL; current = current->Next())
    {
        prev = current;
    }
    prev->SetNext(NULL);
    return;
}

Node* List::Find(int data)
{
    Node* current;

    for (current = head; current != NULL && current->Data() != data; current = current->Next())
    {}
    if(current == NULL)
    {
        cout << "Did not find " << data << "." << endl;
        return NULL;
    }
    else // found
    {
        cout << "Found " << data << "." << endl;
        return current;
    }
}

void List::Print()
{
    Node* current;
    for (current = head; current != NULL; current = current->Next())
    {
        cout << current->Data() << " ";
    }
    cout << endl;

    return;
}

节点
#ifndef NODE_H
#define NODE_H

class Node
{
private:
    int data;
    Node* next;

public:
    Node();
    void SetData(int aData);
    void SetNext(Node* aNext);
    int Data();
    Node* Next();
};

#endif

Node.cpp
#include <cstddef>

#include "Node.h"

Node::Node()
{
    this->SetData(NULL);
    this->SetNext(NULL);
    return;
}

void Node::SetData(int aData)
{
    this->data = aData;
    return;
}

void Node::SetNext(Node* aNext)
{
    this->next = aNext;
    return;
}

int Node::Data()
{
    return data;
}

Node* Node::Next()
{
    return next;
}

最佳答案

第一次调用current-> SetData时(请参见下文),current为NULL,因此在访问它时会出现页面错误(页面错误是现代操作系统在尝试访问未分配的内存时给您的错误。访问冲突。)

void List::Add_End(int data)
{
    Node* current;
    Node* newEnd = new Node();

    for (current = head; current != NULL; current = current->Next())
    {}
    current->SetData(data);
    current->SetNext(newEnd);
    newEnd->SetData(NULL);
    newEnd->SetNext(NULL);

    return;
}

关于c++ - C++ : Attempting to add new node to linked list yields “Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)” error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30136329/

相关文章:

error-handling - 抛出HttpError时ServiceStack不会填充响应DTO

jquery - JSlint : Unexpected string concatenation error

c++ - 正确使用cpp中的定义宏替换函数的名称

ios - 如何从 objective-c 中的日期选择器获取日期?

swift - XC框架 "Cannot load underlying module"

ios - Cordova + iOs 发布错误 ITMS90035 - 签名无效。代码对象根本没有签名

c++ - 将多维数组从 C 返回到 Lua

c++ - QWidget删除问题

c++ - Gdb 在一个简单的 std::string 未捕获异常上没有给出堆栈

exception - 在 Dart 中管理登录/功能错误的最佳做法是什么?