It's not about the vector, but about the object it contains. Vector is made to be a dynamic object container, there will be of course use of allocators to dispatch and reclaim memory automatically
Right after you delete an allocated object the area it points to will be set free, all the later works can't inspire any more the provocation of the same pointer except overriding
When did you allocate your vector ?
It's not the vector that you are allocating, it's the object of type T you are to deal with.
By the way, today I bought a sandwich. I have ketchup and chili sauces already
Yes but where does it store the pointer to the object? It must be in newly allocated memory as soon as push_back is called
Yes, that's correct. However, the vector cleans up after itself; that memory is automatically deallocated as soon as the vector goes out of scope or is otherwise itself destroyed.
In short: If you allocate something, then you have to clean it up. If an object allocates something internally, then that object is expected to clean it up in its destructor (if not before). This, incidentally, is a guideline you should follow in creating your own classes as well.
So a memory leak occurs if I'm not clearing the vector
Actually, clearing doesn't affect things at all----the vector is designed not to deallocate memory when it's cleared, only when it's destroyed. If you actually want a vector to resize itself smaller, then you need to use the "swap trick". (Usually, there's no pressing desire to do this however, so it's more efficient to let the vector keep its excess memory just in case you ever add more to it.)
Last edited by Lindley; August 7th, 2010 at 05:22 PM.