Can I force std::vector to not deallocate its memory after the vector goes out of scope?
For example, if I have
The vector
is desiged to prevent leaks.
But if you want to shoot yourself in the foot, it's possible. Here's how you prevent the vector from deallocating its internal array:
int *foo()
{
std::vector v(10,1);
int *ret = v.data();
new (&v) std::vector; // Replace `v` with an empty vector. Old storage is leaked.
return ret;
}
As the other answers say, you should never do it.