http://www.cplusplus.com/reference/vector/vector/clear/ writes
Clear content
Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.
A reallocation is not guaranteed to happen, and the vector capacity is not guaranteed to change due to calling this function. A typical alternative that forces a reallocation is to use swap:
vector<T>().swap(x); // clear x reallocating |
http://www.cplusplus.com/reference/vector/vector/shrink_to_fit/ writes:
Shrink to fit
Requests the container to reduce its capacity to fit its size.
The request is non-binding, and the container implementation is free to optimize otherwise and leave the vector with a capacity greater than its size. |
These imply that
ebz wrote: |
---|
I indeed get 0 elements after calling .size() |
is mostly irrelevant. The simple
textures.clear() does achieve the
size() == 0. For
allocated memory one should look at
textures.capacity().
The swap idiom does guarantee "reallocation":
1 2 3 4 5 6 7 8 9
|
vector x;
// fill x
{
vector y; // empty
swap(x, y);
// x is now empty and y has capacity
} // deallocate y that owns x's former capacity
// x is truly empty
|
However, a vector of pointers has allocated memory
only for pointers. Very small objects. That vector in itself consumes insignificant amount of memory (unless it has humongous amount of elements).
Even with
vector<Texture>
(which is most likely much smarter than use of smart pointers that in turn trump raw pointers) what do we know about
struct Texture
?
The "test" program has Texture that is nothing more than one
int
. A pointer to Texture is at least as large as the Texture itself.
The test program creates only one Texture object.
The vector has at most one pointer to that one Texture object.
The vector object has a pointer to array, size, capacity, and whatnot in them. They are there whether the vector is empty or not.
The test program allocates (and deallocates) memory for one int and one pointer from free store.
Or put other way, it is difficult to see whether anything did happen, when almost nothing did happen.