0

a 2D and 3D array vectors are defined and used throughout a simulation, they are very large in size so deallocation is neccesary.

array<vector<double>, n> A;
array<vector<vector<double>, n>, m> B;

what is the proper way?

jarhead
  • 1,821
  • 4
  • 26
  • 46

1 Answers1

3

just let them go out of scope:

{
    array<vector<double>, n> A;
    array<vector<vector<double>, n>, m> B;

    // use arrays ...

} // leave scope, arrays will be deallocated
m.s.
  • 16,063
  • 7
  • 53
  • 88
  • what do u mean? when simulation is done they are deallocated? – jarhead Oct 30 '15 at 16:36
  • @jarhead they will be deallocated whenever the scope they are defined in is left – m.s. Oct 30 '15 at 16:38
  • 2
    If you put '{}' around your variable, they are deallocated after the '}'. http://stackoverflow.com/questions/10080935/when-is-an-object-out-of-scope – ARG Oct 30 '15 at 16:41
  • @ARG so basically when it is out of main() it is out of scope as well – jarhead Oct 30 '15 at 18:27
  • 1
    Yes, and also if you put them in code like this `int main() { { array, n> A; array, n>, m> B; // do whatever you want with them } // out of scope so arrays will be deallocated // do whatever else you want return 0; }` – ARG Oct 30 '15 at 18:53
  • But IMHO there is no need to deallocate the variable which are allocated on stack – ARG Oct 30 '15 at 19:00