| | |
Problem deleting pointers from list
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
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?
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)
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() ;
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
1
#2 32 Days Ago
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
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:
The normal process that would be not to use the temporary.
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.
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)
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.
c++ Syntax (Toggle Plain Text)
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.
experience is the most expensive way to learn anything
![]() |
Similar Threads
- Major Problem with Doubly linked list program (C++)
- Make a program in C of the Josephus problem using Circular Linked list (C)
- problem in deleting (PHP)
- Deleting In Linked List.. (C++)
- Problem on array based list (C)
Other Threads in the C++ Forum
- Previous Thread: Client machine prerequisites when installing VS C++ applicaton
- Next Thread: inherit class from structure
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





