模板类的破坏者导致分割故障
这是简短版本的模板类,具有重要的方法:
template<class T>
class Matrix
{
protected:
int width;
int height;
T ** values;
public:
Matrix(int width, int height);
~Matrix();
T get(int i, int j);
void set(int i, int j, T value);
};
template<class T>
Matrix<T>::Matrix(int width, int height) {
this->width = width;
this->height = height;
this->values = new T*[width];
for(int i=0; i<width; i++)
this->values[i] = new T[height];
}
template<class T>
Matrix<T>::~Matrix() {
cout << "~ 1" << endl;
for(int i=0; i<this->width; i++) {
cout << "~ 2" << endl;
for(int j=0; j<this->height; j++) {
cout << "~ 3" << endl;
delete [] &this->values[i][j];
}
cout << "~ 4" << endl;
delete [] &this->values[i];
cout << "~ 5" << endl;
}
}
template<class T>
T Matrix<T>::get(int i, int j) {
return this->values[i][j];
}
template<class T>
void Matrix<T>::set(int i, int j, T value) {
this->values[i][j] = value;
}
在我的主项目中,每次可执行文件需要从此类中删除对象时,程序都会因分段错误错误而退出。
下面是一个也发生错误的小程序:
int main() {
cout << "1" << endl;
Matrix<int> * matrix = new Matrix<int>(300, 300);
cout << "2" << endl;
for(int i=0; i<300; i++)
for(int j=0; j<300; j++)
matrix->set(i, j, i+j);
cout << "3" << endl;
delete matrix;
cout << "4" << endl;
return 1;
}
任何人都可以告诉我这个析构函数有什么问题吗?
This is the short version template class, with the important methods:
template<class T>
class Matrix
{
protected:
int width;
int height;
T ** values;
public:
Matrix(int width, int height);
~Matrix();
T get(int i, int j);
void set(int i, int j, T value);
};
template<class T>
Matrix<T>::Matrix(int width, int height) {
this->width = width;
this->height = height;
this->values = new T*[width];
for(int i=0; i<width; i++)
this->values[i] = new T[height];
}
template<class T>
Matrix<T>::~Matrix() {
cout << "~ 1" << endl;
for(int i=0; i<this->width; i++) {
cout << "~ 2" << endl;
for(int j=0; j<this->height; j++) {
cout << "~ 3" << endl;
delete [] &this->values[i][j];
}
cout << "~ 4" << endl;
delete [] &this->values[i];
cout << "~ 5" << endl;
}
}
template<class T>
T Matrix<T>::get(int i, int j) {
return this->values[i][j];
}
template<class T>
void Matrix<T>::set(int i, int j, T value) {
this->values[i][j] = value;
}
In my main project, every time the executable needs delete an object from this class, the program exits with a segmentation fault error.
Below a small program where the error also occurs:
int main() {
cout << "1" << endl;
Matrix<int> * matrix = new Matrix<int>(300, 300);
cout << "2" << endl;
for(int i=0; i<300; i++)
for(int j=0; j<300; j++)
matrix->set(i, j, i+j);
cout << "3" << endl;
delete matrix;
cout << "4" << endl;
return 1;
}
Anyone can tell me what's wrong with this destructor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的破坏者编码错误。
每个
new []
需要一个相应的delete []
。您的构造函数调用new []
1次分配values
的第一维>values
的第二维度的时间。因此,您的驱动器需要调用
delete []
width
width times times times times times times times times times for 1st维,例如:还请注意,您的
矩阵
class不关注 3/5/0的规则管理value
Pointer 正确,因为它缺少复制构造函数和复制分配运算符,以及移动构造函数和移动分配操作员。因此,如果您的矩阵
对象被复制/移动,这将导致破坏者问题。Your destructor is coded wrong.
Each
new[]
needs a correspondingdelete[]
. Your constructor callsnew[]
1 time to allocate the 1st dimension ofvalues
, and then callsnew[]
width
times for the 2nd dimension ofvalues
.As such, your destructor needs to call
delete[]
width
times for the 2nd dimension, and calldelete[]
1 time for the 1st dimension, eg:Also be aware that your
Matrix
class is not following the Rule of 3/5/0 to manage thevalue
pointer correctly, as it lacks a copy constructor and copy assignment operator, and a move constructor and move assignment operator. So that will cause problems for your destructor if yourMatrix
objects are ever copied/moved.