memory - What is the right way to free a std::vector of pointers in C++? -


I searched the stack overflow but could not find the answer to this question.

Suppose I have study :: vector & lt; Day * & gt; Vector_de - that is - the vector of pointers for the day object I now push_back to vector_day many elements:

< Pre> vector_day.push_back (new day (12)); Vector_day. Push_back (new day (99)); Vector_day.push_back (new day (71)); ...

Now I do not need anymore vector_day at some point. What is the right way to free the memory?

This is the correct way:

 for  (std :: vector  & gt; :: iterator i = vector_day .begin (); i! = Vector_day.end (); ++ i) {Delete * i; }  

Does not this invalid the vector on each extinction? I am very confused.

The best way is not to put the indicator in the first place in the vector if you want to do it completely is not needed.

But if you really need a vector of the indicator, then the way you are doing it is fine (but .clean) after the vector, if It will not be destroyed immediately, then it is not full of dangerous signals)

statement

  delete;  

does not affect the itater, it does not change the itater, invalidates the itater or removes the indicator specified by the iterator from the collection. All this is free that indicates memory that points directed by the iterator. Indicators must be removed separately from the collection.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -