944,116 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 515
  • C++ RSS
Oct 25th, 2009
1

Problem deleting pointers from list

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  1. for(list<Vertex*>::iterator vertexListIterator = vertexList.begin() ; vertexListIterator != vertexList.end(); ++vertexListIterator)
  2. {
  3. Vertex* v = (Vertex*)(*vertexListIterator) ;
  4. delete v ;
  5. v = 0 ;
  6. }
  7.  
  8. for(list<Vertex*>::iterator vertexListIterator = vertexList.begin() ; vertexListIterator != vertexList.end(); ++vertexListIterator)
  9. {
  10. Vertex* v = (Vertex*)(*vertexListIterator) ;
  11. cout << "ID : "<< v->ID <<"\tAddress v : " << (long)(v) << endl ;
  12. }
  13. vertexList.clear() ;
Similar Threads
Reputation Points: 12
Solved Threads: 0
Newbie Poster
schnell is offline Offline
2 posts
since Oct 2009
Oct 25th, 2009
1
Re: Problem deleting pointers from list
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:
c++ Syntax (Toggle Plain Text)
  1. for(list<int>::iterator ai=L.begin();ai!=L.end();++ai)
  2. {
  3. int x= *ai;
  4. x=4; // This doesn't change anything in list
  5. }

The normal process that would be not to use the temporary.
c++ Syntax (Toggle Plain Text)
  1. delete *vertexListIterator;
  2. *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.
Reputation Points: 749
Solved Threads: 135
Practically a Master Poster
StuXYZ is offline Offline
660 posts
since Nov 2008
Oct 26th, 2009
0
Re: Problem deleting pointers from list
Thanks a lot for the tips and explaination. Now I get it what exactly is going on.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
schnell is offline Offline
2 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Client machine prerequisites when installing VS C++ applicaton
Next Thread in C++ Forum Timeline: inherit class from structure





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC