Linux 中的分段错误C++但代码在 Windows 中工作
Segmentation Fault in Linux C++ but code is working in windows
AoA, 这是两个矩阵的乘法代码,它在 3x3 矩阵下运行良好,但在超过 3x3 的行或列时会出错,就像在 3x4 和 4x3 上一样,它给出错误"分割错误"
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
struct matrix
{
int** mat;
int row;
int col;
matrix(int m,int n)
{
row = m;
col = n;
mat = new int*[row];
for( int i=0;i<row;i++ )
{
mat[i] = new int[col];
for( int k=0;k<col;k++ )
{
mat[i][k] = 0;
}
}
}
};
matrix* MultiplyMat(matrix* matA,matrix* matB)
{
matrix* tMat = new matrix(matA->row,matB->col);
if(matA->row == matB->col)
{
for( int i=0; i<matA->row; i++ )
{
for( int j=0;j<matB->col;j++ )
{
for( int m=0;m<matB->col;m++ )
{
tMat->mat[j][i] += matA->mat[j][m] * matB->mat[m][i];
}
}
}
}
return tMat;
}
void PrintMatrix(matrix* tMat)
{
cout<<"Print: Matrixnn";
for( int i=0;tMat->row;i++ )
{
for( int j=0;j<tMat->col;j++ )
{
cout<<" "<<tMat->mat[i][j];
}
cout<<"n";
}
}
int main()
{
matrix matB(3,4);
matrix matA(4,3);
matA.mat[0][0] = 2;
matA.mat[0][1] = 1;
matA.mat[0][2] = 4;
matA.mat[1][0] = 6;
matA.mat[1][1] = 5;
matA.mat[1][2] = 9;
matA.mat[2][0] = 8;
matA.mat[2][1] = 7;
matA.mat[2][2] = 11;
matA.mat[3][0] = 5;
matA.mat[3][1] = 5;
matA.mat[3][2] = 9;
matB.mat[0][0] = 2;
matB.mat[0][1] = 1;
matB.mat[0][2] = 4;
matB.mat[0][3] = 3;
matB.mat[1][0] = 6;
matB.mat[1][1] = 5;
matB.mat[1][2] = 9;
matB.mat[1][3] = 12;
matB.mat[2][0] = 8;
matB.mat[2][1] = 7;
matB.mat[2][2] = 11;
matB.mat[2][3] = 13;
matrix* matC = MultiplyMat(&matA,&matB);
PrintMatrix(matC);
return 0;
}
我只是想多重播放两个矩阵,g++ 编译器给出错误"分段错误"我尝试了调试方法(在此站点上找到),但未能删除错误!
有什么帮助吗?
这一行是错误的:
matrix* tMat = (matrix*)malloc(sizeof(matrix));
我不完全确定你期望它做什么,但它可能不会那样做......实际上,它根本没有做太多事情,除了创建一个足够大的内存块来容纳结构matrix
。它充满了一些随机垃圾(可能是也可能不是零)。
然后你继续使用它:
tMat->mat[j][i] += matA->mat[j][m] * matB->mat[m][i];
这很可能意味着您正在访问 NULL 或一些无效的随机垃圾地址。然后返回指向它的指针,此处未释放该指针:
matrix* matC = MultiplyMat(&matA,&matB);
PrintMatrix(matC);
return 0;
您可能想要类似以下内容:
matrix* tMat = new matrix(matB->col, matA->row);
但是你最好创建一个matrix operator*(const matrix& a, const matrix& b)
,所以你根本不返回指针。开销将非常小。
您的矩阵实际上有一个构造函数,但是在使用 malloc()
分配内存时不会调用它。您显然想使用
matrix* tMat = new matrix(m, n);
与合适的参数m
和n
.好吧,实际上,您宁愿使用
std::unique_ptr<matrix> tMat(new matrix(m, n));
。但这只与克服分段错误后会遇到的下一个问题有关:您还需要清理资源。不过,这也不是你真正想要的,因为你真的想要这样的东西:
matrix MultiplyMat(matrix const& a, matrix const& b) {
// ...
matrix result(m, n);
// ...
return result;
}
相关文章:
- 注释一行使代码工作,而没有它,代码不起作用
- 当我的代码工作正常时,为什么在SPOJ中遇到运行时(Sigabrt)错误
- 回文字符串问题:为什么我必须放+1而不是-1才能让这个代码工作
- 如何使用模板或继承使以下代码工作
- 为什么这个循环运行不可见的代码?工作不正常
- 堆叠C++代码工作正常,但在运行代码结束时"glibc detected"
- 返回引用 - 为什么我的代码工作正常?不应该
- 如何使此代码工作
- 为什么这个单例类代码工作正常
- 对于下面的代码,我将 decltype(s1.size()) 更改为 int,代码工作正常.在这种情况下,decltyp
- 代码工作到一定程度,然后崩溃
- 为什么此代码工作/失败取决于在中创建的范围闭包
- OpenGL代码工作,不管我使用什么输入/窗口处理程序
- c++代码工作正常,但进程以termination结束,而不是返回0
- 为什么代码工作良好,当我编译和运行它,即使我没有提到数组的大小
- 如果元素不能为代码工作
- 为什么这段代码工作正常?(vector.clear(), vector<vector<int> >)
- C++ 昨天分段错误测试的代码工作完美,现在它出错了
- 由于在返回语句中使用了布尔或操作,因此递归如何对以下代码工作
- C /c++ hackerrank,我的代码是工作的,但我得到0.38 /10分(不知道为什么)代码工作