How does a vector know when to destroy itself?
Printable View
How does a vector know when to destroy itself?
It's a class. It doesn't destroy itself, the destructor is called as soon as the class is deleted.
I'm assuming for each element inside a vector it's allocating memory internally.
So, this is never deleted if you dont use the vector anymore? And thus vectors cause memory leaks?
so if you have
Code:vector<T*> v;
T * ptr = new T();
v.push_back( ptr );
delete ptr;
//nothing with v done after this
//leak?
No since the only thing v contains is a pointer to the deleted T object.
Memory does not have to be allocated to store the pointer to the T object?
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
So a memory leak occurs if I'm not clearing the vector
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 :)
vector is allocated on the stack, it can't grow there, so it has to allocate memory to grow and hold elements like my pointer
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.
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.)Quote:
So a memory leak occurs if I'm not clearing the vector
How does a vector know it goes out of scope?
How does any object you create knows it goes out of scope? There is nothing magical about vector -- all objects have a destructor that gets invoked automatically when the object goes out of scope.
Do you know what a destructor is and when it's called? If not, I suggest you read up on basic C++ fundamentals.
All objects that are declared have a scope, block scope, function scope, file scope, etc. When the object goes out of scope, the destructor is automatically invoked. The object doesn't "know" when it is or is not in scope, as it doesn't need to. Everything is taken care of by the language.
Do you understand this concept?Code:void f()
{
int a;
} // when this function is done, a is no longer in scope.
Regards.
Paul McKenzie
Nothing stops a vector implementation from using an array initially for fast access, and if the number of elements exceeds a certain size, switch to dynamically allocated memory. All of that is required of vector is that the memory used to store the items must be contiguous. Where that memory comes from doesn't matter.
All of this shouldn't be of concern to you. The vector knows how to handle its own memory very nicely without outside help. What you should be concerned with is using a vector properly, not how internally it does things.
Regards,
Paul McKenzie
Ah the compiler makes sure constructors and destructors are called for objects. I always thought constructors were only called on 'new' and 'delete'.
Well that makes things clear.
Thanks !