Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to free c++ memory vector<int> * arr?

I have a vector<int>* arr, which is actually a 2D array.

arr =  new vector<int> [size];

Is it ok that I just do

delete arr;

Will arr[i] be automatically be deleted, since it is a standard vector?

like image 379
Michael Avatar asked Dec 03 '22 01:12

Michael


1 Answers

No, you should be using delete[] when you used new[].

But this is madness. You're using nice happy friendly containers for one dimension, then undoing all the goodness by resorting to manual dynamic allocation for the outer dimension.

Instead, just use a std::vector<std::vector<int> >, or flatten the two dimensions into a single vector.

like image 113
Lightness Races in Orbit Avatar answered Dec 19 '22 17:12

Lightness Races in Orbit