首页 > 解决方案 > 为什么我的程序中的矩阵乘法会崩溃?

问题描述

我在我的程序中遇到了一些麻烦。我确定这与内存有关,但我无法确定崩溃的确切原因。在我的程序中,我有两个长浮点矩阵,我想将它们相乘。一个矩阵是 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 来确保矩阵实际上是二维数组并存储在内存中。因为我的程序有点长,所以我将发布与乘法相关的代码片段。我有两个名为 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");

一旦乘法开始,代码就会崩溃。任何帮助,将不胜感激。谢谢 :)

标签: cmatrix

解决方案


推荐阅读