title: Segmentation violation signal in unidirectional list
body: <p>I'm trying to make a unidirectional list with nodes containing a value and a pointer to the next node (the pointer in the final node is supposed to be a nullptr). </p>
  
  <p>Howerver, things are not going as planed. It's compiling without any problems, but when I try to run it i get this fatal error condition:
    SIGSEGV - Segmentation violation signal.</p>
  
  <p>It think it's trying to reach memory which it doesn't have permission to use, or something? Another common cause is an accidental "=" instead of "==", but that doesn't seem to be the problem here.</p>
  
  <p>It seems that the error occurs when I try to construct a Sorted_List without any nodes in my test file, like this:</p>
  
  <pre><code>Sorted_List empty_list{};
  </code></pre>
  
  <p>Here is the code I imagine can be relevant to the error:</p>
  
  <p>Sorted_List.cc</p>
  
  <pre><code>#include "Sorted_list.h"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  using namespace std;
  
  Sorted_List::Sorted_List() : head{nullptr} {}
  
  Sorted_List::Sorted_List(initializer_list&lt;int&gt; i)
    :Sorted_List()
  {
    for (auto ii : i)
      {
        add_val(ii);
      }
  }
  
  Sorted_List::~Sorted_List()
  {
    if (!check_empty())
      {
        Node* del = head;
        while(del != nullptr)
      {
        Node* next = del-&gt;next;
        delete del;
        del = next;
      }
      }
  }
  
  bool Sorted_List::check_empty() const
  {
    return (head-&gt;value == 0 &amp;&amp; head-&gt;next == nullptr);
  }
  
  
  void Sorted_List::del_val(int num)
  {
    Node* del = head;
    if (num == 1)
    {
      head = del-&gt;next;
      delete del;
    }
    for (int i = 1; i &lt; num - 1; i++)
      {
        del = del-&gt;next;
      }
  }
  
  void Sorted_List::add_val(int num)
  {
    Node* temp = new Node;
    temp-&gt;value = num;
    if (head == nullptr || head-&gt;value &gt;= temp-&gt;value)
      {
        temp-&gt;next = head;
        head = temp;
      }    
    else
      {
        Node* current = head;
        while(current-&gt;next != nullptr &amp;&amp; current-&gt;next-&gt;value &lt;temp-&gt;value)
      {
        current = current-&gt;next;
      }
        temp-&gt;next = current-&gt;next;
        current-&gt;next = temp;
      }
  }
  string Sorted_List::print( Sorted_List&amp; list)
  {
    Sorted_List::Node* temp;
    stringstream list_stream;
    for(temp = list.head; temp != nullptr; temp = temp-&gt;next) 
      {
        list_stream &lt;&lt; temp-&gt;value;
        if(temp-&gt;next != nullptr)
      list_stream &lt;&lt; ", ";
      }
    return list_stream.str();
  }
  </code></pre>
  
  <p>Sorted_List.h</p>
  
  <pre><code>#ifndef SORTED_LIST_H
  #define SORTED_LIST_H
  
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;initializer_list&gt;
  #include &lt;string&gt;
  
  class Sorted_List
  {
  private:
      class Node
      {
      public:
          int value{};
          Node* next{};
      };
  Node* head{};
  
  public:
      Sorted_List();
      Sorted_List(std::initializer_list&lt;int&gt;);
      ~Sorted_List();
  
      std::string print(Sorted_List&amp;);
      void add_val(int num);
      bool check_empty() const;
      void del_val(int num);
  };
  
  #endif
  </code></pre>
  
  <p>Sorted_List_test.cc</p>
  
  <pre><code>#define CATCH_CONFIG_MAIN
  #include "Sorted_list.h"
  #include "catch.hpp"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  TEST_CASE(" EMPTY ")
  {  
    Sorted_List empty_list{}; // this is where the error occurs
    //REQUIRE(empty_list.check_empty() == true);
    //REQUIRE(empty_list.print(empty_list) == "");
  }
  </code></pre>
  
  <p>Any clues?</p>