2

I'm dealing with dynamic arrays. The function empty_matrix() creates a new array, representing a matrix. delete_matrix() frees all memory, allocated for the matrix.

Do I get a memory leak in function example() if I call add(add(a, b), c)? What will happen to the memory allocated in the function add(...)? Do I have to free it? Where should I do it?

 matrix empty_matrix(int dim) {
 matrix m;
 m.dim = dim;
 m.data = new int*[dim];
 for (int i = 0; i < dim; i++)
  m.data[i] = new int[dim];

 return m;
}

void delete_matrix(matrix m) {
 for (int i = 0; i < dim; i++)
  delete [] m.data[i];
 delete [] m.data;
}

matrix add(matrix a, matrix b) {
 matrix c = empty_matrix(a.dim);
 for (int i = 0; i < a.dim; i++)
  for (int j = 0; j < a.dim; j++)
   c.data[i][j] = a.data[i][j] + b.data[i][j];

 return c;
}

void example() {
 matrix a = empty_matrix(100);
 matrix b = empty_matrix(100);
 matrix c = empty_matrix(100);

 // some modifications of a, b and c
 // ...

 matrix d = add(add(a, b), c);
 print_matrix(d);

 delete_matrix(a);
 delete_matrix(b);
 delete_matrix(c);
 delete_matrix(d);
} 
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
multiholle
  • 3,050
  • 8
  • 41
  • 60
  • 1
    You can use tools like valgrind: http://valgrind.org/ which can tell you are there leaks. – Mariy Nov 28 '10 at 18:41

3 Answers3

5

What you should do is use Object Orientation/RAII. Your data member of matrix class should be private, and memory for it should be allocated in the constructor, and freed in the destructor. This way, you won't have to worry about memory leaks.

For example...

class matrix
{
public:
      typedef int element_type;
      matrix(int dimension)
          :data_(new element_type[dimension*dimension])
      {

      }  
      //Do define your copy-constructor and assignment operators
      ~matrix()
      {
         delete [] data_;
      } 

private:
      element_type* data_;
};

This all, of course, if this is homework. If it is not, then you should refrain from using arrays in this situation. Use std::vectors

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • With using Object orientation I can't avoid to free the Memory allocated by the `add(...)` function seperatly? Are `std::vector` s as fast as arrays? How do I access the data, via `get(int i, int j)`? – multiholle Nov 28 '10 at 18:59
  • @multiholle: Yes, you will avoid the memory leak, because the temporary object will be destroyes, its destructor will be called and the memory will be freed. As to how to use vector see this link: http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027 – Armen Tsirunyan Nov 28 '10 at 19:05
  • @multiholle: And yes, vectors are *almost* as fast as arrays. There are very few situations in which the difference is noticable. – Armen Tsirunyan Nov 28 '10 at 19:09
  • What does this constructor mean? I don't know what's after the ":". matrix(int dimension) :data_(new element_type[dimension*dimension]) { } – multiholle Nov 28 '10 at 21:09
  • @multiholle: It's called constructor initialization list. Check this out http://www.cprogramming.com/tutorial/initialization-lists-c++.html – Armen Tsirunyan Nov 28 '10 at 21:19
  • I have tryed to implement it with a class matrix, but still have a memory leak: http://stackoverflow.com/q/4300663/519790 Using the destructor will cause a "Segmentation fault". – multiholle Nov 29 '10 at 13:51
2

Yes, you will have to free the result of any empty_matrix or add call using delete_matrix. You're leaking memory, because you're not freeing the matrix of the innermost add call.

Stéphan Kochen
  • 19,513
  • 9
  • 61
  • 50
1

You should have exactly one new for one delete and one new[] for one delete[] in your code. That's the most important principle.

Hence, if the function add creates a new matrix, it needs to be deleted somewhere.


Also, empty_matrix should be the constructor of the matrix class, and delete_matrix should be its destuctor.

You could also replace the data with a std::vector and your memory handling would be much more automated, relieving you from the necessity of counting news and deletes.

Kos
  • 70,399
  • 25
  • 169
  • 233