1

Suppose I have a following Class:

   class foo{

   int array_allocation(int length){

          array= new int[length];
          return 0;

   }
   private:
      int *array;

   };

Here should I need to implement ~foo(){ delete []array}; or its implicitly done??

Mysticial
  • 464,885
  • 45
  • 335
  • 332
N. F.
  • 891
  • 3
  • 9
  • 33
  • 4
    [Yes you need to deallocate it yourself.](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – Mysticial Aug 27 '12 at 21:53
  • 5
    Use `std::vector` and you won't have to worry. To respond to Mysticial's link, [Rule of Zero](http://rmartinho.github.com/2012/08/15/rule-of-zero.html). – chris Aug 27 '12 at 21:53
  • You need to do it manually. But there is a lot more involved than just writting a destructor (that is not enough). You need to apply the rule of three (five). But the standard containers already manage memory for you when you need multiple values. Use `std::vector array;` – Martin York Aug 27 '12 at 21:55
  • Yes...you would use std::vector instead of an array, but the question is not, what is best to use, the question is about the memory management of objects. What if the OP had been allocating something else as an example? – Matthew Layton Aug 27 '12 at 22:07
  • in some of my member functions I declared arrays which depen on the calculated size, so I used new operator.Could I use simply int array[calculted_size] declaration instead?..as I have used new operator I should deallocate memory at the end when the funtion returns right?...I was interested on arrays more than vectors for the reason that they are faster, so I want to stick to arrays – N. F. Aug 27 '12 at 23:05
  • most of my classes I needed to declare a dynamic array as a private variable and member funtions actually allocates them by themselves, what should be the common structure of a copy constructor and assignment operator for that? – N. F. Aug 27 '12 at 23:05
  • While using copy constructors is it needed to copy other member variables which are not dynamically allocated? – N. F. Aug 28 '12 at 01:17

2 Answers2

4

Neither, you should use std::vector<int> instead. I'm not even talking as a member of your class, but instead of your class.

EDIT: no, the memory is not freed automatically. You need to provide a meaningful destructor, copy constructor & assignment operator.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 1
    whilst I fully understand and respect your answer, the question is about memory management. The question does not directly relate to the use of arrays (and as you quite rightly pointed out, vectors), and in that respect, the memory management of an array. What if class foo had been allocating something else? – Matthew Layton Aug 27 '12 at 22:05
  • @activwerx when I answered, there already was an answer covering that part (deleted now), that's why I skipped it. – Luchian Grigore Aug 27 '12 at 22:10
  • Fair play! I just wanted to be sure! :-) – Matthew Layton Aug 27 '12 at 22:11
1

Pointers are not automatically deleted. Also when that class is copied the pointer is copied not the memory. You should observe the RAII design pattern and the rule of three.

stonemetal
  • 6,111
  • 23
  • 25