0

I have questions about the destructing process of STL vector as well as array of vectors. The only thing I learned from my teacher is the number of delete is equal to the number of new. I have 3 questions for the array of vector in different contexts. To be convenient, all of the array has 5 vectors. Each vector has 1 element.

1. Array of vectors appears in main.cpp

int main(){
    int size = 5;
    vector<int> *array_vec = new vector<int>[size];
    for(int i=0; i<size; i++){
        array_vec[i].push_back{i};
    }
    delete [] array_vec;
    return 0;
}

Question 1: At the end of the main() function, I manually delete the array by delete [] array_vec, but I don't manually free each vectors array_vec[i]. Was the array_vec[i]'s memory released automatically? If yes, when? (before OR after my delete [] array_vec?) The reason why I ask this question is for 2D dynamic array, we must free the inner array before the outer array, so I wonder whether it is the case for the array of vector.

2. Array of vectors appears in a class's method as a temporary variable

Class myClass{
public:
    void myMethod(){
       int size = 5;
       vector<int> *array_vec = new vector<int>[size];
       for(int i=0; i<size; i++){
           array_vec[i].push_back(i);
       }
       delete [] array_vec;
    }
}

int main(){
    myClass myclass;
    myclass.myMethod();
    /** some other process in the program **/
    return 0;
}

Question 2: Was the array_vec[i] released before the myclass.myMethod() returned? OR array_vec[i] will only be released at the end of the entire program?

3. Array of vectors appears in a class as a class property

Class myClass{
public:
    vector<int> *array_vec;
public:
    ~myClass(){
        if(array_vec)
            delete [] array_vec;
    }
    void Init(){
       int size = 5;
       array_vec = new vector<int>[size];
       for(int i=0; i<size; i++){
           array_vec[i].push_back(i);
       }
    }

    void Use(){
        std::cout<<array_vec[0][0]<<std::endl;
    }
}

int main(){
    myClass myclass;
    myclass.Init();
    myclass.Use();
    /** some other process in the program **/
    return 0;
}

Question 3: Is my destructor OK? In my ~myClass() method, I just free the array of vectors array_vec, but I do not know whether those vectors themselves will be free automatically or not? If yes, that will be good. If no, when will they be released? How can I manually free those vectors?

This problem has confused me for a long time. The memory issue is very important for my c++ program, because I need to use array of vectors in a big loop. If the memory is not released correctly, the memory usage will be larger and larger while the program is running. In that case, my program will crash. Really appreciate your help.

F.L.
  • 23
  • 4
  • 7
    `new vector[size]` is generally a bad idea, `std::vector>` is fine (or `std::array, 5>`). – Jarod42 Mar 22 '18 at 03:23
  • #3 breaks rule of 3/5/0. – Jarod42 Mar 22 '18 at 03:26
  • I don't understand why you didn't just use `std::vector` throughout your code. The whole purpose of `std::vector` is to not need to do `new[]` and `delete[]`. Doesn't your wording `array of vectors` suggest a vector of vectors? – PaulMcKenzie Mar 22 '18 at 03:32
  • 3
    The use of your `new` and `delete` are all ok, things do get cleaned up properly, but you should use `std::vector>` as Jarod points out and not involve `new` or `delete` at all. – super Mar 22 '18 at 03:32
  • Thank you all of you so much. Now I will change to vector of vector. That is easier to use. – F.L. Mar 22 '18 at 13:17

1 Answers1

0

All of your examples are fine. std::vector takes care of cleaning up the memory it owns. That's why you use it instead of raw pointers.

In all of the cases you posted, you should use a std::vector<std::vector<int>> instead though. That will free you from having to deal with memory management at all, and help avoid easy mistakes, like the rule of three violation in your third example.

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52