Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 双重自由还是腐败——为什么? 类矩阵{ 私人: int n,*wsk; friend istream&operator>>(istream&,matrix&); friend ostream&operatorwsk=new int[this->n*this->n]; 对于(inti=0;iwsk!=0)删除[]此->wsk; } 常数矩阵和运算符=(常数矩阵和矩阵){ 如果(&mat==this)返回*this; 如果(this->wsk!=0)删除[]this->wsk; n=材料n; 这个->wsk=newint[n*n]; for(inti=0;iwsk[i]=mat.wsk[i]; 归还*这个; } }; istream和operator>>(istream和str、matrix和mat){ str>>材料n; 如果(材料编号>0){ 如果(mat.wsk!=0)删除[]mat.wsk; mat.wsk=新整数[mat.n*mat.n]; 对于(int i=0;i>mat.wsk[i]; } 返回str; } ostream&operator_C++_Matrix_Cout_Cin_Double Free - Fatal编程技术网

C++ 双重自由还是腐败——为什么? 类矩阵{ 私人: int n,*wsk; friend istream&operator>>(istream&,matrix&); friend ostream&operatorwsk=new int[this->n*this->n]; 对于(inti=0;iwsk!=0)删除[]此->wsk; } 常数矩阵和运算符=(常数矩阵和矩阵){ 如果(&mat==this)返回*this; 如果(this->wsk!=0)删除[]this->wsk; n=材料n; 这个->wsk=newint[n*n]; for(inti=0;iwsk[i]=mat.wsk[i]; 归还*这个; } }; istream和operator>>(istream和str、matrix和mat){ str>>材料n; 如果(材料编号>0){ 如果(mat.wsk!=0)删除[]mat.wsk; mat.wsk=新整数[mat.n*mat.n]; 对于(int i=0;i>mat.wsk[i]; } 返回str; } ostream&operator

C++ 双重自由还是腐败——为什么? 类矩阵{ 私人: int n,*wsk; friend istream&operator>>(istream&,matrix&); friend ostream&operatorwsk=new int[this->n*this->n]; 对于(inti=0;iwsk!=0)删除[]此->wsk; } 常数矩阵和运算符=(常数矩阵和矩阵){ 如果(&mat==this)返回*this; 如果(this->wsk!=0)删除[]this->wsk; n=材料n; 这个->wsk=newint[n*n]; for(inti=0;iwsk[i]=mat.wsk[i]; 归还*这个; } }; istream和operator>>(istream和str、matrix和mat){ str>>材料n; 如果(材料编号>0){ 如果(mat.wsk!=0)删除[]mat.wsk; mat.wsk=新整数[mat.n*mat.n]; 对于(int i=0;i>mat.wsk[i]; } 返回str; } ostream&operator,c++,matrix,cout,cin,double-free,C++,Matrix,Cout,Cin,Double Free,我看到的一个错误是,在复制构造函数中,您正在删除从未分配的内存: int main(){ matrix mac, a, b; cout << "Put number of dimensions and numbers in matrix "; cin >> mac; cout << mac; cin >> a; cout << a; mac.~matrix();

我看到的一个错误是,在复制构造函数中,您正在删除从未分配的内存:

int main(){
    matrix mac, a, b;
    cout << "Put number of dimensions and numbers in matrix ";  
    cin >> mac;
    cout << mac;
    cin >> a;   
    cout << a;
    mac.~matrix();
    return 0;
}
检查非NULL对您没有帮助。该指针可能有一个非NULL垃圾值,您正在使用垃圾指针调用
delete[]
。只需从复制构造函数中完全删除该行即可

其次,您的赋值运算符有问题:

 this->n=mat.n;
 if (wsk!=0) delete []wsk;
注释解释了问题。您删除了数据,如果以后
new[]
失败,您将无法恢复

您还可以返回
const
。对于赋值运算符返回const对象来说,这是非常规的

编写赋值运算符的更好方法是:

  const matrix & operator=(const matrix &mat){
            if(&mat==this) return *this;

            // you've destroyed your data here
            if (this->wsk!=0) delete [] this->wsk;

            // you've changed one of your members here
            n=mat.n;

            // what if the line below throws a `std::bad_alloc` exception?
            this->wsk=new int [n*n];
编辑:您在
主功能中执行此操作:

mac.~matrix();

您正在显式调用析构函数。那么当
mac
对象超出范围时会发生什么情况呢?析构函数将自动再次调用,因此您会得到双重删除错误


main
中删除这一行。对象的析构函数将被自动调用。

在我看来,delete[]试图为数组的每个元素调用析构函数,然后它会破坏指针。它可能会导致双重错误

你试过替换吗

~matrix(){ delete[]this->wsk; }
和老C malloc

int *foo=new int[n*m]
这样你就可以用delete代替delete[],我希望这能奏效

玩得开心,让我知道


gf

您使用了调试器了吗?它会准确地告诉您为什么会有双重空闲。您也不需要在删除某些内容之前检查null。如果(wsk!=0)delete[]wsk;
在复制构造函数中没有意义。在复制构造函数开始执行之前,对象不存在,因此
wsk
不可能有值得检查的值。步骤1:将
int*
替换为
std::vector
步骤2:profit为什么这样做:
mac.~matrix()
?@kamilose这不是你解决问题的方法——如果评论可以得到否决票,你的最后一条评论也会得到否决票。你永远不应该显式调用析构函数,除非你没有这样做,比如
placement new
。只需从
main
中删除错误的一行,然后离开析构函数即可独自一人。这没有帮助。这仍然是同一个问题。我没有问运算符=。我唯一的一个问题是,我有双重自由。我删除了复制构造函数中的行,删除了运算符=,因为它现在不重要。问题是为什么在“cin>>matrix1”之后,我在其中输入了例如n=2和数字,然后“cin>>matrix2”并将示例n=3的更高维度放入为什么我可以获得双重免费?@KamilOsuch请发布一个main()复制错误的程序。您从未发布过如何测试此类。事实上,您没有发布显示如何实际创建这些
matrix
对象的代码。您发布的只是一个默认构造函数和一个副本构造函数。@Kamilosoke-
我没有询问运算符=。
您有一个运行时问题e、 我们不知道您是如何使用这个类的,因此我们不知道
操作符=
是否在崩溃中起作用。因此,我们修复了我们看到的内容,您的
操作符=
需要修复。
~matrix(){ delete[]this->wsk; }
int *foo=new int[n*m]
int *foo;
foo=(int*)malloc(n*m*sizeof(int));