title: deleting a hash table in C++
body: <p>I am building a hash table using arrays and buckets that are represented by linked lists in C++. I am running into something very weird when I try to clear the hash table and I would appreciate it if someone can explain why this is happening. </p>
  
  <p>This code works fine:</p>
  
  <pre><code>for(int i = 0; i &lt; bins; i++)
  {
      while(map[i]-&gt;next != nullptr)
      {
          LN* toDelete = map[i];
          map[i] = map[i]-&gt;next;
          delete toDelete;
      }
  }
  </code></pre>
  
  <p>However for some reason if I do this, it doesn't delete anything anymore:</p>
  
  <pre><code>for(int i = 0; i &lt; bins; i++)
  {
      LN* node = map[i]
      while(node-&gt;next != nullptr)
      {
          LN* toDelete = node;
          node = node-&gt;next;
          delete toDelete;
      }
  }
  </code></pre>
  
  <p>Each bucker is represented by a trailer linked list that's why I'm checking node->next not node. From my okay understanding of pointers, node should refer to the same thing as <code>map[i]</code> so when I call delete on node it should delete the object that both <code>map[i]</code> and node refer to.</p>
  
  <p>Thank you in advance </p>