Deleting STL list elements
-
Hi all, So I have a list< double* >, which contains arrays which were created with something like new double[4]. When I'm done with the list I need to free all of the elements. At first I tried this:
list< double* > ::iterator it = my_list.begin();
while( it != my_list.end() )
delete [](*it);but that didn't work, with MFC saying the pointer was still valid, I think because the list still contained references to the element. So I figured the best way would be to walk thru the list, removing, then deleting, elements as I go. But I'm a little unsure about the behaviour of iterators in this situation. For example after the following code:
list< double* > ::iterator my_it = my_list.begin();
my_list.pop_front();Would my_it stil be valid? Where would it be pointing to? I need to figure out the quickest way to safely []delete all elements in the list. Any pointers? TIA, Pete
-
Hi all, So I have a list< double* >, which contains arrays which were created with something like new double[4]. When I'm done with the list I need to free all of the elements. At first I tried this:
list< double* > ::iterator it = my_list.begin();
while( it != my_list.end() )
delete [](*it);but that didn't work, with MFC saying the pointer was still valid, I think because the list still contained references to the element. So I figured the best way would be to walk thru the list, removing, then deleting, elements as I go. But I'm a little unsure about the behaviour of iterators in this situation. For example after the following code:
list< double* > ::iterator my_it = my_list.begin();
my_list.pop_front();Would my_it stil be valid? Where would it be pointing to? I need to figure out the quickest way to safely []delete all elements in the list. Any pointers? TIA, Pete
Doh! this code: while( it != my_list.end() ) delete [](*it); should have been: while( it != my_list.end() ) delete [](*it++); I guess I'm too used to good old for loops :) well, I can now just run thru the list, deleting all the elements, then just delete the list. This is probably the quickest way.... right? Pete
-
Hi all, So I have a list< double* >, which contains arrays which were created with something like new double[4]. When I'm done with the list I need to free all of the elements. At first I tried this:
list< double* > ::iterator it = my_list.begin();
while( it != my_list.end() )
delete [](*it);but that didn't work, with MFC saying the pointer was still valid, I think because the list still contained references to the element. So I figured the best way would be to walk thru the list, removing, then deleting, elements as I go. But I'm a little unsure about the behaviour of iterators in this situation. For example after the following code:
list< double* > ::iterator my_it = my_list.begin();
my_list.pop_front();Would my_it stil be valid? Where would it be pointing to? I need to figure out the quickest way to safely []delete all elements in the list. Any pointers? TIA, Pete
Try the following: list< double* >::iterator i; while ((i = my_list.begin()) != my_list.end()) { double *p = *i; my_list.pop_front(); delete [] p; }
-
Try the following: list< double* >::iterator i; while ((i = my_list.begin()) != my_list.end()) { double *p = *i; my_list.pop_front(); delete [] p; }
-
I like to use something like: while ( ! my_list.empty() ) { delete [] my_list.front(); my_list.pop_front(); } Best regards, John
-
Hi all, So I have a list< double* >, which contains arrays which were created with something like new double[4]. When I'm done with the list I need to free all of the elements. At first I tried this:
list< double* > ::iterator it = my_list.begin();
while( it != my_list.end() )
delete [](*it);but that didn't work, with MFC saying the pointer was still valid, I think because the list still contained references to the element. So I figured the best way would be to walk thru the list, removing, then deleting, elements as I go. But I'm a little unsure about the behaviour of iterators in this situation. For example after the following code:
list< double* > ::iterator my_it = my_list.begin();
my_list.pop_front();Would my_it stil be valid? Where would it be pointing to? I need to figure out the quickest way to safely []delete all elements in the list. Any pointers? TIA, Pete
-
Yeah, that looks nice Do you think it'd be quicker this way... for( i = my_list.size(); i >= 0; i-- ) { delete [] my_list.front(); my_list.pop_front(); } Im guessing the lack of a function call would speed it up, what do you think? Pete
-
The speed difference would be insignificant. The members of list are generally inlined, so don't add any function call overhead. John
-
>>The speed difference would be insignificant. Whereas the time wasted while I try to understand my 'efficient' function in two weeks would be significant... I think I'll go with your version ;) Thanks John
My original reply was written assuming that you can't change the type of the list. If you can change the type of the list, I would do that instead. I would use: list< vector< double > > my_list. Then, when you erase any elements inside the list, the vector will automatically clean up after itself. John
-
My original reply was written assuming that you can't change the type of the list. If you can change the type of the list, I would do that instead. I would use: list< vector< double > > my_list. Then, when you erase any elements inside the list, the vector will automatically clean up after itself. John
-
I _could_ change the type, but it would be more [bureaucratic] effort than it's worth. Thanks anyway, Pete
For future reference though, a vector of doubles in a list would have been a much nicer solution. I usually delete pointers if I need to carry them ( because of polymorphism ) using a for_each and a function object that calls delete on everything in my container. Christian I am completely intolerant of stupidity. Stupidity is, of course, anything that doesn't conform to my way of thinking. - Jamie Hale - 29/05/2002 Half the reason people switch away from VB is to find out what actually goes on.. and then like me they find out that they weren't quite as good as they thought - they've been nannied. - Alex, 13 June 2002
-
I _could_ change the type, but it would be more [bureaucratic] effort than it's worth. Thanks anyway, Pete
Even I don't see much sense in storing double* instead of double in a vector. It will also cause Heap fragmentation and your code may cause more page faults as each double may be scattered all over the memory. Unless you have very important reason against it, change it to double.
-
I _could_ change the type, but it would be more [bureaucratic] effort than it's worth. Thanks anyway, Pete