I have "Vertex" class pointers in a list. I want to delete these pointers and free up the memory. I deleted the pointers in the first for loop and set these pointers to 0. but just to check whether the pointers have really been deleted and set to NULL, I accessed those pointers again in the second loop.

Why am I able to access them and access the attributes and members functions in the second loop when i have set their pointers to NULL in the first loop? Have I really freed up the memory? Is "vertexList.clear()" a save operation to do?

for(list<Vertex*>::iterator vertexListIterator = vertexList.begin() ; vertexListIterator != vertexList.end(); ++vertexListIterator)
{       
      Vertex* v = (Vertex*)(*vertexListIterator) ;
      delete v ;	    
      v = 0 ;	    
}

for(list<Vertex*>::iterator vertexListIterator = vertexList.begin() ; vertexListIterator != vertexList.end(); ++vertexListIterator)
{
      Vertex* v = (Vertex*)(*vertexListIterator) ;
      cout << "ID : "<< v->ID <<"\tAddress v : " << (long)(v) << endl ;
}
vertexList.clear() ;
Comments
Good first post.

The problem you have is temporary values and what happens to them:
I think this will be clear (I hope :)

The problem is that your are taking a copy of the value in the list. This does indeed correctly delete the object. BUT then you do v=0 rather
than *vertexListIterator = 0 ; . Now you access the value in the list, which you have not erased, and then you access the memory area that you have deleted BUT that memory space is marked as deleted but is unlikely to have been over written so you get valid results. If you had done lots of other operations then you might get different results.

Note that if the list was just list<int> and the values were 1,2,3, you would not expect this to change the list:

for(list<int>::iterator ai=L.begin();ai!=L.end();++ai)
{
    int x= *ai;
    x=4;             // This doesn't change anything in list
}

The normal process that would be not to use the temporary.

delete *vertexListIterator;
*vertexListIterator=0

Also please please don't use the C style casts, use a static_cast. That would remove any problems that might arise in-case the list turned out to be of a polymorphic class, and Vertex wasn't the base.

You use reinterpret_cast<long int> in the output of the pointer value to be printed.

Comments
Good Explaination

Thanks a lot for the tips and explaination. Now I get it what exactly is going on.

This question has already been answered. Start a new discussion instead.