[英]How to properly deallocate 2d array c++
we just received the results of our final exam in C++. 我们刚刚收到了C ++期末考试的结果。 One of the questions was to write a simple representation of 2-dimensional matrix of
double
. 问题之一是编写
double
的二维矩阵的简单表示形式。 I had some points deduced at the destructor implementation with the note reading invalid implementation
and no further information. 我对析构函数的实现有一些推断,注解显示
invalid implementation
,没有进一步的信息。 Could you please tell me where I went wrong (if I did indeed go wrong). 您能告诉我哪里出了问题吗(如果我确实确实出了错)。
This is the code I wrote: 这是我写的代码:
~Matrix() {
for (int i=0; i<_rows; i++) {
for (int j=0; j<_cols; j++) {
delete _arr[i][j];
}
delete _arr[i];
}
delete [] _arr;
}
The code for allocating the memory is pretty straight forward, I'll paste it to make things clearer, but the problematic part is in the code above. 分配内存的代码非常简单,我将其粘贴以使内容更清楚,但是有问题的部分在上面的代码中。
Matrix(int rows, int cols) :
_arr(new double*[rows]), _rows(rows), _cols(cols)
{
for (int i=0; i<_rows; i++) {
_arr[i] = new double[_cols];
}
}
Did they rightfully take away the points? 他们理所当然地带走了积分吗? Was my
dtor
implementation indeed invalid? 我的
dtor
实施确实无效吗?
The is wrong and shouldn't be there; delete _arr[i][j]
是错误的,不应该在那里; delete _arr[i][j]
delete _arr[i];
is also wrong - since this is an array that you allocated with new []
, you need to use delete [] _arr[i];
这也是错误的-因为这是您使用
new []
分配的数组,所以需要使用delete [] _arr[i];
. 。
The rule of thumb is: a delete
for each new
; 经验法则是:每个
new
的delete
; and a delete[]
for each new[]
. 以及每个
new[]
的delete[]
new[]
。
Also the delete
s of composites are to be executed in reverse order compared to the new
s (which you did already). 与
new
s(您已经做过)相比,组合的delete
s也将以相反的顺序执行。
Thus we get 这样我们得到
~Matrix() {
for (int i = 0; i < _rows; i++) {
delete[] _arr[i];
}
delete[] _arr;
}
You should call delete[]
for your every new[]
, they should be pairing. 您应该为每个
new[]
调用delete[]
new[]
,它们应该配对。
~Matrix() {
for (int i=0; i<_rows; i++) {
delete[] _arr[i];
}
delete [] _arr;
}
You have two new
then three delete
. 您有两个
new
然后三个delete
。 What's wrong should be obvious. 出了什么问题应该很明显。
Besides, you have to use delete [] for both dimensions, since you have an array of double-array and then an array of double (double*[] and double[]), that lead you to : 此外,您必须对两个维度都使用delete [],因为您有一个double-array数组,然后是double(double * []和double [])数组,这将导致您:
~Matrix() {
for (int i = 0; i < _rows; i++) {
delete[] _arr[i];
}
delete[] _arr;
}
It's not necessary to destroy each value of the array of double, since they have'nt been created by a new
不必破坏double数组的每个值,因为它们不是由
new
数组创建的
Moreover, a simplest implementation uses only one allocation : 而且,最简单的实现仅使用一种分配:
Matrix(int rows, int cols) : _arr(new double[rows*cols]), _rows(rows), _cols(cols)
{
}
~Matrix() {
delete[] _arr;
}
In this implementation, you should provide a correct accessor, since matrix[i,j] = _arr[i+j*row]
or matrix[i,j] = _arr[i*cols+j]
(depends on the way you arrange the values in the array). 在此实现中,您应提供正确的访问器,因为
matrix[i,j] = _arr[i+j*row]
或matrix[i,j] = _arr[i*cols+j]
(取决于您的排列方式数组中的值)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.