title: Copy Constructor and/or pop method in framework for a stack class C++
body: <p>So I have a stack that is made up of randomly generated strings and then a pointer to the next item in my .cpp code. I then go through and pop each item off the stack and print them out. I get a seg fault at the end though so I'm guessing I'm trying to pop an item one past the stack where I do not own the memory.</p>
  
  <p>I believe my copy constructor is incorrect - maybe it doesn't set the last value to null in my stack but I can't figure out why it's not setting the value to NULL when I put the line</p>
  
  <p>newPrev->next = NULL; </p>
  
  <p>here's my code (class only)</p>
  
  <pre><code>    #include &lt;string&gt;
      using namespace std;
      class Stack 
      {
      protected:
          struct Node 
          {
              string item;
              Node* next; 
          }; // struct Node
  
      public:
          // constructor of an empty stack
          Stack ()
          {
              head = NULL;
          }
  
          // copy constructor
          Stack( const Stack &amp; rhs )
          {
              if (rhs.head == NULL) {// check whether original is empty
                  head = NULL; 
              }else{
                  head = new Node;
                  head-&gt;item = rhs.head-&gt;item;
                  Node* newPrev = head;
                  // Now, loop through the rest of the stack
                  for(Node* cur = rhs.head-&gt;next; cur != NULL; cur = cur-&gt;next)
                  {   
                      newPrev-&gt;next = new Node;
                      newPrev = newPrev-&gt;next;
                      newPrev-&gt;item = cur-&gt;item;
                  } // end for 
  
                  newPrev-&gt;next = NULL;
  
              } // end else
          }
  
          // destructor
          ~Stack ()
          {
              delete head; 
          }
  
          // assignment
          const Stack &amp; operator=( const Stack &amp; rhs )
          {
              return *this;
          }
  
          // query whether the stack is empty
          bool empty () const
          {
              return false;
          }
  
          // add an item to the top of the stack
          // this method is complete and correct
          void push (const string &amp; new_item)
          {
              Node* new_node = new Node;
              new_node-&gt;item = new_item;
              new_node-&gt;next = head;
              head = new_node;
          }
  
          // remove the item on the top of the stack
          void pop ()
          {
              if (head!=NULL){
                  Node *n = head;
                  head = head-&gt;next;
                  delete n;
              } 
          }
  
          // return the item on the top of the stack, without modifying the stack
          string &amp; top () const
          {
              return head-&gt;item;
          }
  
      private:
          Node* head;
      };
  </code></pre>