Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly destroy c++ vector of vectors and release memory [duplicate]

Possible Duplicate:
How to release std::vector if there is no heap memory

c++ noob here

Suppose I declare a vector of vectors like:

vector<vector<vector<double> > > phi_x;

After I'm done using it, how do I destroy it to free up memory?

I've read some other sites and they recommend using .clear(). From my understanding, this only deletes the elements but the vector capacity is not released. I want the vector to be completely forgotten like it never existed.

Also, my current application of the vector has dimensions of 54x54x9 and I've got about 12 of these things. Is that considered stupidly big?

like image 705
Keenan Z Avatar asked Dec 03 '12 08:12

Keenan Z


People also ask

How do you destroy a vector in C++?

The C++ function std::vector::clear() destroys the vector by removing all elements from the vector and sets size of vector to zero.

Does vector erase free memory?

Yes. vector::erase destroys the removed object, which involves calling its destructor.

Does delete [] work on vectors?

Yes. For each time a new[] expression is executed, there must be exactly one delete[] .


1 Answers

You don't have to do anything. Just let the variable go out of scope.

like image 147
melpomene Avatar answered Sep 24 '22 18:09

melpomene