The vector will delete, construct, and copy whatever type it contains. In the case of a vector of pointers to a class/structure, it will delete, construct, and copy pointers, leaving the actual objects the pointers point to alone. It is up to you to allocate and deallocate these.
EDIT
An example:
If you have the following:
class A
{
A() {}
}
void foo(void)
{
A * pointerToA = new A;
}
At the end of the function foo's scope the only thing that is deallocated is the memory for the variable pointerToA
itself, i.e. 4 bytes that hold an address (in 32 bit) - which in this case is stored on the stack. The only way that the memory allocated for a new instance of class A will be freed is if you manually call delete with the address to pointerToA
.
Let's take the example of an array of class A
A ** arrayOfPointerToA = new A*[10];
for(unsigned i = 0; i < 10; ++i)
arrayOfPointerToA[i] = new A;
which is similar to what happens when you have std::vector<A*>
. When you call
delete [] arrayOfPointerToA;
you're deallocating the memory for the array of pointers, not for each A
.
In the above diagram, the memory deallocated by the above call to delete is highlighted in red. Note that each A
is stored at a random location in memory in this instance since they were all allocated separately.
Now taking this to a vector:
A std::vector<A>
effectively uses new A[size]
to allocate memory. If you're storing a raw pointer, this would mean it would allocate an array of type A, which means that size
number of objects of type A
are created. When the vector frees its memory size
number of objects of type A
are destroyed. Now take that example and replace A with A* and you'll see that no objects of type A are destroyed.
This is a fundamental part of how C++ and pointers work, not just a property of containers. If containers did arbitrarily call delete on each member, this wouldn't make sense as when we have a container A
we would call delete on an instance of an object instead of a pointer to that object which is not valid.