C 为什么我的程序中的矩阵乘法会崩溃?

C 为什么我的程序中的矩阵乘法会崩溃?,c,matrix,C,Matrix,我的程序出了点问题。我确信这与记忆有关,但我无法准确地找出车祸的确切原因。在我的程序中,我有两个长浮点矩阵,我想将它们相乘。一个矩阵是7x1(7行1列),另一个是7x7(7行7列)。这两个矩阵分别命名为matrixY和matrixY 矩阵T: 49244824435.000000 119389046816.250000 39946687622.000000 134033754390.000000 113174866996.000000 272947307571.500000 5714517393

我的程序出了点问题。我确信这与记忆有关,但我无法准确地找出车祸的确切原因。在我的程序中,我有两个长浮点矩阵,我想将它们相乘。一个矩阵是7x1(7行1列),另一个是7x7(7行7列)。这两个矩阵分别命名为matrixY和matrixY

矩阵T:

49244824435.000000 119389046816.250000 39946687622.000000 134033754390.000000 113174866996.000000 272947307571.500000 57145173936.250000
119389046816.250000 289454411315.062500 96845750191.250000 324960870933.750000 274388194250.500000 661757833373.125000 138543299809.062500
39946687622.000000 96845750191.250000 32404329394.000000 108725307556.000000 91805134479.000000 221408041345.500000 46355176893.250000
134033754390.000000 324960870933.750000 108725307556.000000 364823702850.000000 308047197273.000000 742934555194.500000 155537281593.750000
113174866996.000000 274388194250.500000 91805134479.000000 308047197273.000000 260106770582.000000 627313081608.000000 131331845278.500000
272947307571.500000 661757833373.125000 221408041345.500000 742934555194.500000 627313081608.000000 1512933380437.250000 316738287317.125000
57145173936.250000 138543299809.062500 46355176893.250000 155537281593.750000 131331845278.500000 316738287317.125000 66313171264.062500
矩阵Y:

3.000000
3.000000
2.000000
4.000000
3.000000
4.000000
3.000000
这两个矩阵都来自程序开始时的输入文件。我使用printf来确保矩阵实际上是2D数组并存储在内存中。因为我的程序有点长,所以我将发布与乘法相关的代码片段。我有两个名为row和dimension的变量,用于查找第一个矩阵的行和维度

int main(int argc, char *argv[])
{

    int row;
    int column;
   /*code that takes input from file and scans into matrix*/

   /*code that finds dimensions of matrix and stores in a variable **row** and **column***/
   /*more code that performs other unrelated tasks*/

    /*CREATE PROFUCT MATRIX AND ALLOCATE MEMORY*/
    double **productMatrixY;
    printf("created product matrix Y: \n");

    productMatrixY = (double **)malloc(row*sizeof(double));
    for(i = 0; i < row; i++)
    {
        productMatrixY[i] = malloc(column*sizeof(double)); /*original was column instead of row*/
    }

    /*Multiply both matrices*/

    sum = 0;

    for(i = 0; i < row; i++)
    {
        for(j = 0; j < row; i++) /*originally 'row'*/
        {
            productMatrixY[i][j] = 0;
            for(k = 0; k < column; k++)
            {
                sum += matrixT[i][k] * matrixY[k][j];
            }
            productMatrixY[i][j] = sum;
            sum = 0;
        }
    }
    printf("\n");
    printf("multiplied TRANSPOSE and MATRIX Y\n");
    {
        productMatrixY[i] = malloc(column*sizeof(double)); 
    }
    printf("allocated memory for product matrix Y:\n");
    printf("\n");
intmain(intargc,char*argv[])
{
int行;
int列;
/*从文件中获取输入并扫描到矩阵中的代码*/
/*查找矩阵维度并存储在变量**行**和**列中的代码***/
/*执行其他无关任务的更多代码*/
/*创建产品矩阵并分配内存*/
双重母性;
printf(“创建的产品矩阵Y:\n”);
productMatrixY=(双**)malloc(行*大小(双));
对于(i=0;i

乘法一开始,代码就崩溃了。如果您能提供任何帮助,我们将不胜感激。谢谢:)

看起来您在尝试访问行之后正在分配行??您乘法
sum+=matrixT[i][k]*matrixY[k][j]但是MatrixY不是一维的吗?那么你希望使用两个索引做什么呢?第一个
malloc
行带有
productMatrixY=malloc(row*sizeof(double));
应该带有
sizeof(double*)
您需要在上做更多的工作。在
printf
之后大括号中的
malloc
毫无意义。在第二个for循环中,
for(j=0;j
您正在递增
i
而不是
j