简体   繁体   中英

Do I need to delete a vector?

I am defining a vector as a private variable of a class Grid. Class Points has only two instance vars that are all ints but the number of points will only be known when I read this in from the file, so I guess I have to make Points dynamically with new which means I have to destroy them later. Did I initialize constructor correctly and When writing a destructor for Grid do I need to write a destructor for vector like this: ~vecotr() or with delete or using iterator?

class Grid{
public:
  // initialize vector with 3 points with val 0.
  Grid(vector<Points> v) : vector<Points>(3, 0) {};  // is this right


// first option
  ~Grid() {
    ~vector<Points>();  // not sure how to destroy vector<Points>;
  }

// second option
  ~Grid() {
     delete v_points;
  }

// third option
  ~Grid() {
     for (vector<Points>::iterator it = v_points.begin(), 
          vector<Points>::iterator it_end = v_points.end(); it != it_end; it++)
  }

private:
  vector<Points> v_points;
};

Which option should I use and did I initialize constructor correctly?

If object is not allocate with new , you do not need to explicitly destroy it. The member objects will be automatically destroyed in the reverse order of their declaration. In your case there is no need to even create a destructor as the automatic one will suffice.

If for some reason you do have member objects allocated with new, you also have to create custom copy construct and assignment operator otherwise you run into troubles of sharing the same member object across multiple instances.

I will reply because predecessors answered only to the question from topic while you are asking more questions and I will also give some advice. In the remainder of this post we will assume that if Points do allocate any dynamic memory, the memory is returned properly when Points are deleted.

Class Points has only two instance vars that are all ints but the number of points will only be known when I read this in from the file, so I guess I have to make Points dynamically with new which means I have to destroy them later.

This comes with contradiction to what you actually do here

class Grid{
public:
  // initialize vector with 3 points with val 0.
  Grid(vector<Points> v) : vector<Points>(3, 0) {};  // is this right

private:
  vector<Points> v_points;
};

because you create vector without new . However this might be OK if we assume that you first get number of Points and then you are about to create a Grid. std::vector is not C array and might be easily resized, assigned and gives much more flexibility. You do not have to create it on heap because you are afraid about size: vector elements are always created on heap, it is just a vector itself that is (if it is) on stack. And this is often exactly what we want (see ie RAII ). The proper way to initialize a vector would be then

class Grid{
    public:
      // initialize vector with 3 Points with val Points(0)
      Grid(vector<Points> v) : v_points(3, Points(0)) {};

    private:
      vector<Points> v_points;
};

Note, we do it in initialization list. For POD class members, it makes no difference, it's just a matter of style. For class members which are classes, then it avoids an unnecessary call to a default constructor.

Did I initialize constructor correctly and When writing a destructor for Grid do I need to write a destructor for vector like this: ~vecotr() or with delete or using iterator?

You don't initialize constructor, rather in constructor initializer list you initialize class members. Since vector is not allocated with new , you will not delete it (call delete ). It will be destroyed automatically when Grid is destroyed.

// fourth (and correct) option:
~Grid() {
}

Or just leave out the destructor entirely; the compiler-generated destructor will do the right thing here.

If you create an object with new you must delete it. If you don't, you mustn't.

I am assuming that Points does not have any dynamically allocated memory. If this is the case then all you need is

~Grid() { }

If it does then you need to delete that dynamically allocated memory for each item in the vector (or use smart pointers).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM