title: deallocating memory in a map with pointers
body: <p>I was trying to erase pointer elements (the value in the map is a pointer) from the map and I saw the code  here <a href="https://stackoverflow.com/questions/433164/what-happens-to-an-stl-iterator-after-erasing-it-in-vs-unix-linux/433207#433207">What happens to an STL iterator after erasing it in VS, UNIX/Linux?</a></p>
  
  <pre><code>for(map&lt;T, S*&gt;::iterator it = T2pS.begin(); it != T2pS.end(); T2pS.erase(it++)) {
      // wilhelmtell in the comments is right: no need to check for NULL. 
      // delete of a NULL pointer is a no-op.
      if(it-&gt;second != NULL) {
          delete it-&gt;second;
          it-&gt;second = NULL;
      }
  }
  </code></pre>
  
  <p>I am not sure if the 'delete it->second' with de-allocate the correct memory  because the erase(it++) step already moves the iterator to the next object. By the time, it reaches the delete statement, it is pointing to the next element which we don't want to delete. Am I missing something?</p>