If I have an std::vector that I want to completely overwrite (not just resize). What is the safest way to do this in terms of memory management? For example
int main() {
std::vector<float> X (5, 5.0);
X = std::vector<float> (6, 6.0);
X = std::vector<float> (4, 4.0);
}
will create a vector of 5 floats of value 5.0. Then it will overwrite it with a vector of size 6 with values 6.0, then overwrite with a vector of size 4 with values 4.0. If I'm performing this type of operation an indefinite number of times, are there risks of corrupting or leaking memory with this approach? Should I be using clear()
before each overwrite? Is there a more efficient way to achieve this?
I'm sure this question has been asked many times but Google isn't pulling up the exact scenario I am looking for.
Thanks!