C++ 复制构造函数中的内存分配错误(c+;+;)
我有个问题 我已经在C++中写了相当一段代码。我正在使用MS Visual Studio 2010 它是一个类C++ 复制构造函数中的内存分配错误(c+;+;),c++,memory,constructor,matrix,numeric,C++,Memory,Constructor,Matrix,Numeric,我有个问题 我已经在C++中写了相当一段代码。我正在使用MS Visual Studio 2010 它是一个类矩阵,几乎没有简单的数值函数 以下是实现: //matrix.h #pragma once #define EPS pow(10., -12.) #include <iostream> #include <iomanip> #include <cmath> using namespace std; class matrix { private:
矩阵
,几乎没有简单的数值函数
以下是实现:
//matrix.h
#pragma once
#define EPS pow(10., -12.)
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class matrix
{
private:
unsigned int n; //number of columns
unsigned int m; //number of rows
double* T;
public:
matrix ();
matrix (unsigned int _n);
matrix (unsigned int _n, unsigned int _m);
~matrix ();
matrix (const matrix& A);
matrix operator = (const matrix& A);
unsigned int size_n () const;
unsigned int size_m () const;
void ones ();
void zeros ();
void identity ();
void push (unsigned int i, unsigned int j, double v);
void lu ();
void gauss ();
double det ();
void transposition ();
void inverse ();
bool symmetric ();
bool diag_strong_domination ();
void swap_rows (unsigned int i, unsigned int j);
matrix gauss_eq (matrix b);
friend bool operator == (const matrix A, const matrix B);
friend matrix operator + (const matrix A, const matrix B);
friend matrix operator * (const matrix A, const matrix B);
double operator () (unsigned int i, unsigned int j) const;
friend ostream& operator << (ostream& out, const matrix& A);
};
matrix::matrix () : n(0), m(0)
{
this->T = NULL;
}
matrix::matrix (unsigned int _n) : n(_n), m(_n)
{
if (0==_n)
{
this->T = NULL;
return;
}
this->T = new double [(this->n)*(this->m)];
for (unsigned int i=0; i<this->n*this->m; i++)
this->T[i] = (double)0;
}
matrix::matrix (unsigned int _n, unsigned int _m) : n(_n), m(_m)
{
if (0==_m || 0==_n)
throw "Error: Wrong matrix dimensios";
this->T = new double [n*m];
for (unsigned int i=0; i<n*m; i++)
this->T[i] = (double)0;
}
matrix::~matrix ()
{
delete this->T;
}
matrix::matrix (const matrix& A) : n(A.n), m(A.m)
{
this->T = new double [n*m]; //(double*)malloc(A.m*A.n*sizeof(double));
for (unsigned int i=0; i<n*m; i++)
this->T[i] = A.T[i];
}
matrix matrix::operator= (const matrix& A)
{
if (!(*this==A))
{
this->m = A.m;
this->n = A.n;
delete this->T;
this->T = new double [A.n*A.m];
for (unsigned int i=0; i<A.n*A.m; i++)
this->T[i] = A.T[i];
}
return *this;
}
unsigned int matrix::size_n () const
{
return this->n;
}
unsigned int matrix::size_m () const
{
return this->m;
}
void matrix::ones ()
{
for (unsigned int i=0; i<(this->m)*(this->m); i++)
(*this).T[i] = double(1);
return;
}
void matrix::zeros ()
{
for (unsigned int i=0; i<(this->m)*(this->m); i++)
(*this).T[i] = double(0);
return;
}
void matrix::identity ()
{
if (this->m!=this->n)
throw "Error: Matrix have to be square (identity)";
for (unsigned int i=0; i<(this->m)*(this->m); i++)
(*this).T[i] = double(0);
for (unsigned int k=1; k<=this->m; k++)
(*this).push(k, k, (double)1);
return;
}
void matrix::push (unsigned int i, unsigned int j, double v)
{
if (i<=0 || i>this->m || j<=0 || j>this->n)
throw "Error: Indeks out of range (push)";
this->T[(i-1)*this->n + (j-1)] = v;
}
void matrix::lu ()
{
if (this->m!=this->n)
throw "Error: Matrix have to be square (lu)";
if ((*this).diag_strong_domination())
{
//Doolittle decomposition
matrix L(this->m);
matrix U(this->m);
for (unsigned int b=1; b<=this->m; b++)
L.push(b, b, (double)1);
for (unsigned int b=1; b<=this->m; b++)
U.push(1, b, (*this)(1,b));
for (unsigned int b=2; b<=this->m; b++)
{
for (unsigned int c=1; c<=this->m; c++)
{
for (unsigned int k=1; k<=b-1; k++)
{
double s1 = 0;
if (1==k)
s1 = (double)0;
else
for (unsigned int p=1; p<=k-1; p++)
s1 += L(b,p) * U(p,k);
double v = ((*this)(b,k) - s1)/U(k,k);
L.push(b, k, v);
}
for (unsigned int k=b; k<=this->m; k++)
{
double s2 = 0;
for (unsigned int p=1; p<=b-1; p++)
s2 += L(b,p) * U(p,k);
double v = (*this)(b,k) - s2;
U.push(b, k, v);
}
}
}
for (unsigned int p=1; p<=this->m; p++)
L.push(p, p, (double)0);
(*this) = L + U;
for (unsigned int x=0; x<(*this).m*(*this).n; x++)
{
if (abs((*this).T[x])<EPS)
(*this).T[x] = (double)0;
}
return;
}
(*this).gauss();
return;
}
void matrix::gauss()
{
//LU decomposition (gauss elimination with partal choice of main element)
unsigned int n = (*this).m;
matrix U(*this);
matrix svr(1,n);
for (unsigned int a=1; a<=n; a++)
svr.push(a, 1, a);
for (unsigned int k = 1; k<=(n-1); k++)
{
//main element choice - column
unsigned int max = k;
for (unsigned int q=k; q<=n; q++)
{
if (abs(U(q,k)) > abs(U(max,k)))
max = q;
}
unsigned int p = max;
svr.push(k, 1, p);
if (abs(U(p,k)) < EPS)
throw "Error: det = 0";
//main element swap
if (p!=k)
U.swap_rows(p, k);
//elimination
for (unsigned int i=(k+1); i<=n; i++)
{
double tmp = U(i,k)/U(k,k);
for (unsigned int j=(k+1); j<=n; j++)
{
double v = U(i,j) - tmp * U(k,j);
U.push(i, j, v);
}
}
}
if (abs(U(n,n)) < EPS)
throw "Error: det = 0";
for (unsigned int s=2; s<=n; s++)
for (unsigned int t=1; t<=(s-1); t++)
U.push(s, t, (double)0);
matrix T = (*this);
matrix Uinv(U);
Uinv.inverse();
matrix L(n);
for (unsigned int i=1; i<=n; i++)
for (unsigned int j=1; j<=n; j++)
for (unsigned int k=1; k<=n; k++)
{
double v = T(i,k) * Uinv(k,j);
L.push(i, j, v);
}
//reversing rows swap
for (unsigned int t=1; t<=n; t++)
{
if (t!=svr(t,1))
L.swap_rows(t, svr(t,1));
}
(*this) = L + U;
for (unsigned int k=1; k<=n; k++)
(*this).push(k, k, (*this)(k,k) - (double)1);
for (unsigned int x=0; x<(*this).m*(*this).n; x++)
{
if (abs((*this).T[x])<EPS)
(*this).T[x] = (double)0;
}
return;
}
double matrix::det ()
{
if (this->m!=this->n)
throw "Error: Matrix have to be square (det)";
double det = 1;
matrix TMP = (*this);
TMP.lu();
for (unsigned int i=1; i<=this->m; i++)
det *= (double)(TMP(i,i));
return det;
}
void matrix::transposition()
{
matrix R(*this);
for (unsigned int i=1; i<=(*this).m; i++)
for (unsigned int j=1; j<=(*this).n; j++)
(*this).push(j, i, R(i,j));
return;
}
void matrix::inverse ()
{
unsigned int n = (*this).m;
matrix A(*this);
matrix X(n);
matrix b(1,n);
for (unsigned int i=1; i<=n; i++)
{
b.zeros();
b.push(i, 1, (double)1);
X = A.gauss_eq(b); //error when using inverse in gauss function, used in lu
for (unsigned int k=1; k<=n; k++)
(*this).push(i, k, X(k,1));
}
for (unsigned int x=0; x<(*this).m*(*this).n; x++)
if (abs((*this).T[x])<EPS)
(*this).T[x] = (double)0;
return; //error when calling inverse
}
bool matrix::diag_strong_domination()
{
for (unsigned int i=1; i<=n; i++)
{
double s = (double)0;
for (unsigned int j=1; j<=n; j++)
{
if (j!=i)
s += abs((*this)(i,j));
}
if (s>=abs((*this)(i,i)))
return false;
}
return true;
}
void matrix::swap_rows (unsigned int i, unsigned int j)
{
if (i<=0 || i>this->m || j<=0 || j>this->n)
throw "Error: Indeks out of range (swap_rows)";
matrix R(*this);
for (unsigned int p=1; p<=this->m; p++)
for (unsigned int q=1; q<=this->n; q++)
{
if (p==i)
(*this).push(p, q, R(j,q));
if (p==j)
(*this).push(p, q, R(i,q));
}
return;
}
matrix matrix::gauss_eq (matrix b)
{
matrix A(*this);
unsigned int n = this->m;
for (unsigned int k=1; k<=n-1; k++)
{
unsigned int max = k;
for (unsigned int q=k; q<=n; q++)
{
if (abs(A(q,k)) > abs(A(max,k)))
max = q;
}
unsigned int p = max;
if (abs(A(p,k)) < EPS)
throw "Error: det = 0 (gauss_eq)";
if (p!=k)
{
A.swap_rows(p,k);
b.swap_rows(p,k);
}
for (unsigned int i=k+1; i<=n; i++)
{
double tmp = A(i,k) / A(k,k);
for (unsigned int j=k+1; j<=n; j++)
A.push(i, j, A(i,j) - tmp*A(k,j));
b.push(i, 1, b(i,1) - tmp*b(k,1));
}
if (abs(A(n,n)) < EPS)
throw "Error: det = 0 (gauss_eq)";
}
matrix X(1,n);
double s = 0;
for (unsigned int i=n; i>=1; i--)
{
for (unsigned int j=i+1; j<=n; j++)
s = s + (A(i,j)*X(j,1));
X.push(i, 1, (b(i,1)-s)/A(i,i));
s = 0;
}
return X;
}
bool operator == (const matrix A, const matrix B)
{
if (A.size_m()!=B.size_m() || A.size_n()!=B.size_n())
return false;
for (unsigned int i=1; i<=A.size_m(); i++)
for (unsigned int j=1; j<=A.size_n(); j++)
if (A(i,j)!=B(i,j))
return false;
return true;
}
matrix operator + (const matrix A, const matrix B)
{
if (A.m!=B.m || A.n!=B.n)
throw "Error: Wrong dimensions";
matrix R(A.n, A.m);
for (unsigned int i=0; i<A.m*A.n; i++)
R.T[i] = A.T[i] + B.T[i];
return R;
}
matrix operator * (const matrix A, const matrix B)
{
if (A.n!=B.m)
throw "Error: Wrong dimensions";
matrix R(A.m,B.n);
for (unsigned int i=1; i<=R.m; i++)
for (unsigned int j=1; j<=R.n; j++)
for (unsigned int k=1; k<=A.n; k++)
{
double v = R(i,j) + A(i,k) * B(k,j);
R.push(i, j, v);
}
return R;
}
double matrix::operator () (unsigned int i, unsigned int j) const
{
if (i<=0 || i>this->m || j<=0 || j>this->n)
throw "Error: Indeks out of range (operator)";
return this->T[(i-1)*this->n + (j-1)];
}
ostream& operator << (ostream& out, const matrix& A)
{
if (0==A.size_m() || 0==A.size_n())
{
out<<endl<<" [ ]"<<endl;
return out;
}
int s = 10;
out<<endl<<" [ ";
for (unsigned int i=1; i<=A.size_m(); i++)
{
for (unsigned int j=1; j<=A.size_n(); j++)
out<<" "<<setw(s)<<left<<A(i,j)<<" ";
if (i!=A.size_m())
out<<endl<<" ";
}
out<<" ] "<<endl;
return out;
}
我在函数逆中返回值时出错。当我进入时,我转到destrutor,在释放内存时出错
然而,这并不是全部
当我调用函数lu
时,会出现另一种奇怪的情况,如下所示:
//MatLab.cpp
#include <iostream>
#include <complex>
#include <typeinfo>
using namespace std;
#include "matrix.h"
int main()
{
try
{
matrix S(4);
for (unsigned int i=1; i<=S.size_m(); i++)
for (unsigned int j=1; j<=S.size_n(); j++)
S.push(i, j, (double)3);
for (unsigned int i=1; i<=S.size_m(); i++)
S.push(i, i, (double)0);
cout<<"S:"<<S<<endl;
S.lu(); //<--- here is the problem
cout<<endl<<"LU decomposition"<<S<<endl;
}
catch (char* xcp)
{
cout<<endl<<xcp<<endl<<endl;
}
system("pause");
return 0;
}
参数大小等于68
我不知道会出什么问题
问题是在类矩阵中的构造函数或函数中,还是在我使用的C库中的函数中
我希望有人花时间研究这个问题,尽管有很多代码需要检查
谢谢你的提示。我还没有读完你所有的代码,但这肯定是错的:
matrix::~matrix ()
{
delete this->T;
}
您需要调用delete[]
操作符,而不是普通的delete
<代码>新建
必须始终与删除
匹配,并且新建[]
必须始终与删除[]
匹配。失败是未定义的行为,通常会导致某种内存损坏,从而导致程序崩溃
同样地,operator=
的实现也应该调用delete[]
也不需要在每个成员访问之前加上
this->
或(*this)。
。它不是惯用的C++,只适用于有局部变量遮蔽成员变量的情况下(它本身并不总是很好的实践)。 < p>我没有读过所有的代码,但这绝对是错误的:
matrix::~matrix ()
{
delete this->T;
}
您需要调用delete[]
操作符,而不是普通的delete
<代码>新建必须始终与删除
匹配,并且新建[]
必须始终与删除[]
匹配。失败是未定义的行为,通常会导致某种内存损坏,从而导致程序崩溃
同样地,operator=
的实现也应该调用delete[]
也不需要在每个成员访问之前加上
this->
或(*this)。
。它不是惯用的C++,只适用于有局部变量遮蔽成员变量的情况下(它本身并不总是很好的实践)。 第一,方式,方式太多代码。但问题很简单,;在分配给
new[]
的数组上调用delete
。使用new[]
分配的任何内容都必须处理delete[]
。您在操作符=
中也有同样的问题。首先,代码太多了。但问题很简单,;在分配给new[]
的数组上调用delete
。使用new[]
分配的任何内容都必须处理delete[]
。您在operator=
中也有同样的问题,您需要使用适当的管理类,而不是自己执行。既然您已经自己做了2D索引,那么使用std::vector将是一件好事。您需要使用适当的管理类,而不是自己做。既然您已经自己做了2D索引,那么使用std::vector将是一件好事。您的赋值运算符是错误的,您在其中编写了
matrix matrix::operator=(const matrix& A)
{
if (!(*this==A))
你真的想写
matrix& matrix::operator=(const matrix& A)
{
if (this != &A)
您应该检查两个对象的地址是否不相等,而不是矩阵本身是否不相等(运算符=也应返回引用)
但无论如何,这种类型的赋值运算符是不好的。有一种更好的编写赋值运算符的方法,称为复制和交换习惯用法。它不仅更正确,而且更容易写。请参见这里的示例
您的赋值运算符错误,您在其中写入了
matrix matrix::operator=(const matrix& A)
{
if (!(*this==A))
你真的想写
matrix& matrix::operator=(const matrix& A)
{
if (this != &A)
您应该检查两个对象的地址是否不相等,而不是矩阵本身是否不相等(运算符=也应返回引用)
但无论如何,这种类型的赋值运算符是不好的。有一种更好的编写赋值运算符的方法,称为复制和交换习惯用法。它不仅更正确,而且更容易写。请参见这里的示例
好的。我做了一些更改,现在我可以这样释放内存:
//MatLab.cpp
#include <iostream>
#include <complex>
#include <typeinfo>
using namespace std;
#include "matrix.h"
int main()
{
try
{
matrix S(4);
for (unsigned int i=1; i<=S.size_m(); i++)
for (unsigned int j=1; j<=S.size_n(); j++)
S.push(i, j, (double)3);
for (unsigned int i=1; i<=S.size_m(); i++)
S.push(i, i, (double)0);
cout<<"S:"<<S<<endl;
S.inverse(); //<--- here is the problem
cout<<endl<<"S^(-1) = "<<S<<endl;
}
catch (char* xcp)
{
cout<<endl<<xcp<<endl<<endl;
}
system("pause");
return 0;
}
matrix::~matrix ()
{
delete [] this->T;
}
及
在dbgheap.c中
确定。我做了一些更改,现在我可以这样释放内存:
//MatLab.cpp
#include <iostream>
#include <complex>
#include <typeinfo>
using namespace std;
#include "matrix.h"
int main()
{
try
{
matrix S(4);
for (unsigned int i=1; i<=S.size_m(); i++)
for (unsigned int j=1; j<=S.size_n(); j++)
S.push(i, j, (double)3);
for (unsigned int i=1; i<=S.size_m(); i++)
S.push(i, i, (double)0);
cout<<"S:"<<S<<endl;
S.inverse(); //<--- here is the problem
cout<<endl<<"S^(-1) = "<<S<<endl;
}
catch (char* xcp)
{
cout<<endl<<xcp<<endl<<endl;
}
system("pause");
return 0;
}
matrix::~matrix ()
{
delete [] this->T;
}
及
在dbgheap.c
中,我敢打赌,如果你用std::vector T
代替double*T
,你的大部分问题都会解决。你能给我们展示一下你的构造函数和析构函数的实现,以及可能的反函数的实现吗?指向您正在使用的double的指针是可疑的,但是如果没有看到使用它的代码,就很难判断这是否是问题所在。首先,您的析构函数以及赋值运算符中的delete
错误。如果可以,按照@BenjaminLindley的建议去做。所有的实现都在第一个代码框中。我打赌,如果你用std::vector T
代替double*T
,你的大部分问题都会消失。你能给我们展示一下你的构造函数和析构函数的实现吗,可能还有反函数的实现?指向您正在使用的double的指针是可疑的,但是如果没有看到使用它的代码,就很难判断这是否是问题所在。首先,您的析构函数以及赋值运算符中的delete
错误。如果可以的话,按照@BenjaminLindley的建议去做。所有的实现都在第一个代码框中。仔细想想,说赋值运算符错了实际上是错的。这是不寻常的,它可以改进,但它不是不正确的。我没有