-5

I am quite new to c++ and I have a question.

If I have a class containing a (pointer to a) vector:

class myClass {
public:
    int* direction;
    myClass(int d){direction=new int[d];}
}

When I create an object and delete it in the main:

int main(){
    int d;
    myClass* myvec;
    myvec = new myClass(d);
    delete myvec;
}

Also the destructor for myvec->direction has been coherently called and the memory freed? Or do I have to write an apposite method for that?

I hope the question is clear...

Thomas
  • 301
  • 3
  • 18
  • 3
    You don't have a _class containing a vector_. You have a class containing a pointer. Use `std::vector`, then you will be fine. Or, `std::unique_ptr` if you care about vector memory overhead. – Daniel Langr Nov 26 '18 at 18:58
  • for `int* vector ;` you will need a destructor. Search for the rule of 3 / 5 / 0 and/or just use `std::vector`. – drescherjm Nov 26 '18 at 19:00
  • 4
    Deleting of an object implies *destruction* of it's members. In the case of a raw pointer that does **not** include `delete`ing what it points to (the `vector`pointer is *destroyed* but that is different from calling `delete vector;`). Avoid raw owing pointers (pointers you need to remember to `delete`) when possible in favor of smart pointers (like `std::unique_ptr`) or standard containers (like `std::vector`) depending on the case. – François Andrieux Nov 26 '18 at 19:00
  • Thanks all for your comments ! So automatic deallocation will be in place by substituting " int* vector -> std::vector vector ". Did I understand well ? – Thomas Nov 26 '18 at 19:04
  • ( by the way, I do not understand why "-x" to my question. I explicetly said I am a beginner and since you all answered the same thing I guess the question was clear... boh...) – Thomas Nov 26 '18 at 19:07
  • 1
    I did not downvote. Remember the purpose of a question is to help future readers with the same problem. StackOverflow is not a forum. 3 people voted to say this question is not a good teaching aid. – drescherjm Nov 26 '18 at 19:08
  • 2
    @Thomas 1) The rule of thumb is: `delete` what you `new` (in addition to `delete[]` what you `new[]`), and avoid using `new` where it is not necessary. 2) If you are a beginner, you should learn from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), not by coding randomly. – Algirdas Preidžius Nov 26 '18 at 19:08
  • 1
    That would be `std::vector vector;`. Note : I would recommend not using `vector` as the name of your member as it's also the name of a standard type. It could make the code needlessly confusing to read, reason and talk about. – François Andrieux Nov 26 '18 at 19:17
  • Thx for your comments. I tried to improve the question as well following your suggestions. – Thomas Nov 26 '18 at 19:28

1 Answers1

4

If you've allocated memory with new you need to delete it too, like this:

class myClass {
      int* direction;
public:
      myClass(int d) : direction(new int[d]) {}
      ~myClass() {
          delete[] direction;
      }
}

But you'd also need to write a copy constructor and copy assignment operator, and in C++11 and later also a move constructor and move assignment operator, for this to be working good. Otherwise, you'd risk the default versions of those copying the raw pointer when you use instances of this class.

Take a look at the rule of three/five/zero.

You'd be much better off using a std::vector<int> instead of a raw pointer.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108