【发布时间】:2020-08-08 01:41:35
【问题描述】:
class matrix
{
public:
int nrow;
int ncol;
double **m;
matrix(int r, int c)
{
nrow = r; ncol = c;
m = (double**)malloc(nrow*sizeof(double*));
for(int i=0; i<nrow; i++)
m[i] = (double*)malloc(ncol*sizeof(double));
for(int i=0; i<nrow; i++)
for(int j=0; j<ncol; j++)
m[i][j] = 0;
}
~matrix()
{
for(int i=0; i<nrow; i++)
{
printf("destructor loop: %d\n", i); fflush(stdout);
free(m[i]);
}
free(m);
}
matrix(const matrix& that)
{
this->nrow = that.nrow;
this->ncol = that.ncol;
this->m = (double**)malloc(nrow*sizeof(double*)); // dynamic mem allocation
for(int i=0; i<this->nrow; i++)
m[i] = (double*)malloc(ncol*sizeof(double));
for(int i=0; i<that.nrow; i++)
for(int j=0; j<that.ncol; j++)
this->m[i][j] = that.m[i][j];
}
void operator=(const matrix &that)
{
this->nrow = that.nrow;
this->ncol = that.ncol;
for(int i=0; i<nrow; i++) // clear current m
free(m[i]);
free(m);
this->m = (double**)malloc(nrow*sizeof(double*)); // dynamic mem allocation
for(int i=0; i<this->nrow; i++)
m[i] = (double*)malloc(ncol*sizeof(double));
for(int i=0; i<that.nrow; i++)
for(int j=0; j<that.ncol; j++)
this->m[i][j] = that.m[i][j];
}
void show()
{
printf(" Marix:\n");
for(int i=0; i<nrow; i++)
{
printf(" ");
for(int j=0; j<ncol; j++)
{
if(m[i][j] >= 0) printf(" ");
printf("%.6lf ", m[i][j]);
}
printf("\n");
}
printf("\n");
}
matrix cofactor(int r, int c) // returns cofactor of a[r][c]
{
printf("cofactor: size:(%d, %d) element:(%d, %d)\n", nrow, ncol, r, c); fflush(stdout);
matrix ans(nrow-1, ncol-1);
int x = 0, y = 0;
for(int i=0; i<nrow; i++)
{
if(i == r) continue;
for(int j=0; j<ncol; j++)
{
if(j == c) continue;
ans.m[x][y] = m[i][j];
y++;
}
x++;
}
return ans;
}
double det()
{
if(nrow != ncol)
{
printf("non-square matrix: (%d, %d)\n\n", nrow, ncol);
exit(1);
}
if(nrow == 1) return m[0][0];
if(nrow == 2)
{
double ans = m[0][0]*m[1][1] - m[1][0]*m[0][1];
return ans;
}
double ans = 0;
int sign = 1;
for(int i=0; i<nrow; i++)
{
printf("det[size:%d, %d] loop row: %d\n", nrow, ncol, i); fflush(stdout);
matrix a(2, 2);
a = cofactor(i, 0);
ans += (sign*a.det());
sign = sign*(-1);
}
return ans;
}
};
int main()
{
matrix c(3, 3);
c.m[0][0] = 2; c.m[0][1] = -3; c.m[0][2] = 1;
c.m[1][0] = 2; c.m[1][1] = 0; c.m[1][2] = -3;
c.m[2][0] = 1; c.m[2][1] = 4; c.m[2][2] = 5;
c.show();
printf("determinant: %lf\n", c.det());
return 0;
}
在对 3x3 矩阵调用 det 函数时会发生分段错误。 我没有使用复杂调试器的经验,但使用 printf 语句看起来运行时错误发生在析构函数中的循环迭代期间。我也尝试过使用向量>而不是双**。我认为在这种情况下它不需要析构函数。然后它给了我一个“双重免费错误”。我了解它何时发生,但在我的代码中找不到错误。我也尝试过计算内联辅因子而不是调用函数,但它没有帮助。
【问题讨论】:
-
我怀疑
void operator=(const matrix &that)重载正在释放析构函数也释放的东西。 -
使用
-fsanitize=address编译程序,当您出现内存错误时获得更多信息。 -
在你的赋值运算符中,你设置
this->nrow = that.nrow在你释放你的旧数组。这意味着在that.nrow != this.nrow的任何情况下,您都在错误的行数上调用free() -
@JeremyFriesner 是的,对不起,我解决了这个问题。不幸的是,它没有解决错误。
-
@narutouzumaki798 尝试在您的每个
malloc()呼叫之后放置一个printf("@%i I just allocated %p\n", __LINE__, theReturnValueFromMalloc);,并在您的每个free()呼叫之前放置一个printf("@%i I'm about to free(%p)\n", __LINE__, theValueYouAreAboutToPassToFree);呼叫。然后运行您的程序,并将其 stdout 重定向到一个文件,然后进入该文件,对于每个“我只是 malloced(X)”行,找到下一个匹配的“我即将释放(X)”行和删除它们。一旦你完成了尽可能多的工作,任何“我即将释放”行仍然是你的错误。
标签: c++ segmentation-fault c++17 double-free