简体   繁体   English

在C ++中为数组正确分配和释放内存

[英]Correct allocate and free memory for arrays in C++

I'm dealing with dynamic arrays. 我正在处理动态数组。 The function empty_matrix() creates a new array, representing a matrix. 函数empty_matrix()创建一个表示矩阵的新数组。 delete_matrix() frees all memory, allocated for the matrix. delete_matrix()释放为矩阵分配的所有内存。

Do I get a memory leak in function example() if I call add(add(a, b), c) ? 如果我调用add(add(a, b), c)我是否在函数example()出现内存泄漏? What will happen to the memory allocated in the function add(...) ? 在函数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);
} 

What you should do is use Object Orientation/RAII. 应该做的是使用面向对象/ 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::vector s 使用std::vector s

Yes, you will have to free the result of any empty_matrix or add call using delete_matrix . 是的,您必须释放任何empty_matrix的结果或使用delete_matrix add调用。 You're leaking memory, because you're not freeing the matrix of the innermost add call. 你正在泄漏内存,因为你没有释放最里面的add调用的矩阵。

You should have exactly one new for one delete and one new[] for one delete[] in your code. 在代码中,一个delete[]应该只有一个new delete ,一个delete[]应该有一个new[] That's the most important principle. 这是最重要的原则。

Hence, if the function add creates a new matrix, it needs to be deleted somewhere. 因此,如果函数add创建了一个新矩阵,则需要在某处删除它。


Also, empty_matrix should be the constructor of the matrix class, and delete_matrix should be its destuctor. 另外, empty_matrix应该是matrix类的构造函数,而delete_matrix应该是它的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 new s and delete s. 您还可以使用std::vector替换data ,并且您的内存处理将更加自动化,从而使您免于计算new s和delete s的必要性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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