title: C++: Attempting to add new node to linked list yields "Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)" error
body: <p>I'm writing a program for a homework assignment that creates and manipulates a linked list. I am encountering an "EXC_BAD_ACCESS" error with the Node::SetData function in Node.cpp, as well as with a line in List::Add_End in List.cpp (specifically "current->SetData(data);") and a line in main.cpp for some reason (specifically "// Add_End nodes to the list"). I assume that once the Node::SetData error is fixed, these other errors will resolve themselves.</p>
  
  <p>After searching through Stack Overflow and Google, I cannot determine why this error is occurring. I thought this question (<a href="https://stackoverflow.com/questions/15074436/new-to-c-exc-bad-access-error-i-dont-understand">New to C++, &quot;EXC_BAD_ACCESS&quot; error I don&#39;t understand</a>) would help, but I'm still having issues.</p>
  
  <p>What coding error(s) have I made?</p>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstddef&gt;
  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 &lt;&lt; "Testing Add_Front: and others" &lt;&lt; endl;
      list.Add_Front(888);
      list.Print();
      list.Add_Front(999);
      list.Print();
      list.Add_Front(49);
      list.Print();
  
      cout &lt;&lt; "Checking find function" &lt;&lt; endl;
      answer = list.Find(888);
      cout &lt;&lt; "Value for node returned by find function call with 888 is " &lt;&lt; answer-&gt;Data() &lt;&lt; "." &lt;&lt; endl;
      cout &lt;&lt; "Checking find function" &lt;&lt; endl;
      answer = list.Find(999);
      cout &lt;&lt; "Value for node returned by find function call with 888 is " &lt;&lt; answer-&gt;Data() &lt;&lt; "." &lt;&lt; endl;
      cout &lt;&lt; "Checking find function" &lt;&lt; endl;
      answer = list.Find(49);
      cout &lt;&lt; "Value for node returned by find function call with 888 is " &lt;&lt; answer-&gt;Data() &lt;&lt; "." &lt;&lt; endl;
      cout &lt;&lt; "Call find function with value not in list." &lt;&lt; endl;
      answer = list.Find(7);
      if (answer == NULL)
      {
          cout &lt;&lt; "returned null pointer since 7 not found" &lt;&lt; endl;
      }
      else
      {
          cout &lt;&lt; "in else of answer == NULL where Value for node returned by find function call with 7 is " &lt;&lt; answer-&gt;Data() &lt;&lt; "." &lt;&lt; endl;
      }
  
      cout &lt;&lt; "testing delete_front: " &lt;&lt; endl;
      list.Delete_Front();
      list.Print();
      cout &lt;&lt; "testing delete_end: " &lt;&lt; endl;
  
      list.Delete_End();
      list.Print();
  
      return 0;
  }
  </code></pre>
  
  <p>List.h</p>
  
  <pre><code>#ifndef LIST_H
  #define LIST_H
  
  #include &lt;cstddef&gt;
  
  #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
  </code></pre>
  
  <p>List.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstddef&gt;
  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-&gt;Next())
      {}
      current-&gt;SetData(data);
      current-&gt;SetNext(newEnd);
      newEnd-&gt;SetData(NULL);
      newEnd-&gt;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-&gt;SetData(data);
      newNode-&gt;SetNext(head);
      head = newNode;
      return;
  }
  
  void List::Delete_End()
  {
      if (head == NULL)
      {
          cout &lt;&lt; "List has no member so cannot delete end" &lt;&lt; endl;
          return;
      }
  
      // check if one in length
      if (head-&gt;Next() == NULL)
      {
          head = NULL;
          return;
      }
      // 2 or greater in length
  
      Node* current;
      Node* prev;
      prev = head;
      for (current = head-&gt;Next(); current-&gt;Next() != NULL; current = current-&gt;Next())
      {
          prev = current;
      }
      prev-&gt;SetNext(NULL);
      return;
  }
  
  Node* List::Find(int data)
  {
      Node* current;
  
      for (current = head; current != NULL &amp;&amp; current-&gt;Data() != data; current = current-&gt;Next())
      {}
      if(current == NULL)
      {
          cout &lt;&lt; "Did not find " &lt;&lt; data &lt;&lt; "." &lt;&lt; endl;
          return NULL;
      }
      else // found
      {
          cout &lt;&lt; "Found " &lt;&lt; data &lt;&lt; "." &lt;&lt; endl;
          return current;
      }
  }
  
  void List::Print()
  {
      Node* current;
      for (current = head; current != NULL; current = current-&gt;Next())
      {
          cout &lt;&lt; current-&gt;Data() &lt;&lt; " ";
      }
      cout &lt;&lt; endl;
  
      return;
  }
  </code></pre>
  
  <p>Node.h</p>
  
  <pre><code>#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
  </code></pre>
  
  <p>Node.cpp</p>
  
  <pre><code>#include &lt;cstddef&gt;
  
  #include "Node.h"
  
  Node::Node()
  {
      this-&gt;SetData(NULL);
      this-&gt;SetNext(NULL);
      return;
  }
  
  void Node::SetData(int aData)
  {
      this-&gt;data = aData;
      return;
  }
  
  void Node::SetNext(Node* aNext)
  {
      this-&gt;next = aNext;
      return;
  }
  
  int Node::Data()
  {
      return data;
  }
  
  Node* Node::Next()
  {
      return next;
  }
  </code></pre>