vector::clear() necessary?
I have an stl vector array which stores the pointers to objects.
To delete the array i am using:
std::vector<*foo> bar;
...
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
{
delete itr;
itr = NULL;
}
Do i need to clear the bar array after this using bar.clear();
if i do not use bar.clear() waht could be the implications.
Quote:Original post by rish2005That doesn't look right. I would think you'd want:
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
{
delete itr;
itr = NULL;
}
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); ++itr){ delete (*itr);}
In any case, it's not strictly necessary to call clear() after performing this step; the memory allocated by the vector itself will be released when the vector goes out of scope.
You may want to consider a vector of smart pointers a boost::ptr_vector instead of a std::vector of raw pointers.
I agree with jyk and SiCrane but do consider, which I think the AP might have been trying to point out, that if that vector is not going out of scope and there is ANY possibility that you might be checking it agiain, then you definately want to clear it.
You dont want to end up trying to access those null pointers later on down the line and not knowing why the heck its crashing.
You dont want to end up trying to access those null pointers later on down the line and not knowing why the heck its crashing.
Well I always clear the array, after deallocating it. Because I just want to be sure that I can't access those NULL pointers.
The only thing I can think of(at this point)why you wouldn't want to clear it, is when you insert new items to the vector.
GBS
The only thing I can think of(at this point)why you wouldn't want to clear it, is when you insert new items to the vector.
GBS
Uh, I surely can't be the only one to notice that 'vector<*foo>' should be 'vector<foo*>'? :s
Quote:Original post by Zahlman
Uh, I surely can't be the only one to notice that 'vector<*foo>' should be 'vector<foo*>'? :s
I myself would use foo* too, is *foo legal? If so, is there a difference?
no its not legal (you cant dereference a data type, only a pointer to an allocated instance of a data type) in that case... its a typo none of us caught.
Quote:Original post by jykQuote:Original post by rish2005That doesn't look right. I would think you'd want:for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); ++itr)
for (vector<*foo>::iterator itr = bar.begin(); itr != bar.end(); )
{
delete itr;
itr = NULL;
}
{
delete (*itr);
}In any case, it's not strictly necessary to call clear() after performing this step; the memory allocated by the vector itself will be released when the vector goes out of scope.
Is this valid??
when using a std::map it has a special property that deleting an element does not invalidate the iterator, but this is not the case which a vector. If you delete the element pointed to by the iterator can you then increment the iterator??
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement