1

I'm trying to write this Linked List class for my assignment and I'm trying to write a "for_each" function in the class that gives the user read-only access to the data in each node. However when I try to access the data in the nodes I get an error saying, "EXC_BAD_ACCESS(code=1, address=0x0)" How can I get access to my data without leaking memory? I assume that is what the error is referring to.

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include <iostream>
#include <memory>

//template<typename T>
class LinkedList
{
private:

    struct Node
    {
        int data;
        std::shared_ptr<Node> next;

        Node(int d, std::shared_ptr<Node> n)
        :data(d)
        ,next(n)
        {}
        Node()
        {};
    };

    std::shared_ptr<Node> head;
    std::shared_ptr<Node> temp;
    std::shared_ptr<Node> current;

public:

    LinkedList()
       :head()
    {}

    LinkedList(LinkedList& other)
       :head(Clone(other.head))
    {}

    std::shared_ptr<Node> getStart()
    {
        return head;
    }

    void InsertAt(int value, std::shared_ptr<Node> &n)
    {
        n->next = std::make_shared<Node>(value, n->next);

    }

    void Insertion(int value)
    {
        Insertion(value, head);
    }

    void Insertion(int value, std::shared_ptr<Node> &n)
    {
        if (!n)
        {
            InsertAt(value, n);
            return;
        }

        if (value < n->data)
            Insertion(value, n->next);
        else
            InsertAt(value, n);
    }

    void Remove(int value)
    {
        Remove(value, head);
    }

    void Remove(int value, std::shared_ptr<Node>& n)
    {
        if (!n) return;
        if (n->data == value)
        {
            n = n->next;
            Remove(value, n);
        }
        else
        {
            Remove(value, n->next);
        }
    }

    void for_each(std::shared_ptr<Node> n)
    {
        if(!n) return;

        std::cout<<current->Node::data;  <---- //Here it keeps telling me I have bad_access
        for_each(current->next);               //"EXC_BAD_ACCESS(code=1, address=0x0)

    }

    std::shared_ptr<Node> Clone(std::shared_ptr<Node> n) const
    {
        if(!n) return nullptr;
        return std::make_shared<Node>(n->data, Clone(n->next));
    }

    LinkedList& operator = (const LinkedList& list)
    {
        this->Clone(list.head);
        return *this;
    }
};

#endif

1 Answer 1

1

Not sure why you're using current in your for_each contract, In fact, I see no reason for current in any of this code, and sometimes, recursion isn't the solution:

void for_each(std::shared_ptr<Node> n)
{
    if(!n) return;

    std::cout<<current->Node::data;  <---- this is never set to anything
    for_each(current->next);
}

Try this:

void for_each(std::shared_ptr<Node> n)
{
    while(n)
    {
        std::cout << n->data << ' ';
        n = n->next;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.