【问题标题】:Sum of matrices in a returning matrix function返回矩阵函数中的矩阵和
【发布时间】:2021-05-11 08:25:26
【问题描述】:

下面是一个可编译的程序,其中大部分是由 Craig Estey 制作的,它帮助我解决了另一个问题。我只是使用它,以便人们可以测试程序,但我面临的问题是使用一个函数来求和两个矩阵。准确来说是函数

Matrix* sum(Matrix* mat1, Matrix* mat2){
    int row, col, k, l;
    Matrix *mat3;
    mat3 = allocateMemory(mat1->row, mat1->col);

    if((mat1->row == mat2->row)&&(mat1->col == mat2->col)){ //verify if the two matrices have the same order
        for (row = 0; row < mat1->row; row++){
            for (col = 0; col < mat1->col; col++){
                k = row * mat1->col + col;//create index for matrix 1 element
                l = row * mat2->col + col;//create index for matrix 2 element
                mat3->a[k] = mat1->a[k] + mat2->a[l]; //sum elements and store in mat3
            }
        }
        return mat3;
    }
}

可编译的C程序

#include <stdio.h>
#include <stdlib.h>

typedef struct matrix {
    int row;
    int col;
    int *a;
} Matrix;

Matrix *allocateMemory(int row, int col)
{
    Matrix *mat = malloc(sizeof(*mat));

    if (mat == NULL) {
        perror("malloc");
        exit(1);
    }

    mat->row = row;
    mat->col = col;

    mat->a = calloc(row * col, sizeof(*mat->a));
    if (mat->a == NULL) {
        perror("calloc");
        exit(1);
    }

    return mat;
}

void printMatrix(Matrix *mat)
{
    int row, col, off;

    for (row = 0; row < mat->row; row++) {
        for (col = 0; col < mat->col; col++) {
            off = (row * mat->col) + col;
            printf("%d   ", mat->a[off]);
        }
        printf("\n");
    }
}

void matrix_fill(Matrix *mat)
{
    int row, col, off;
    int val = 1;

    for (row = 0; row < mat->row; row++) {
        for (col = 0; col < mat->col; col++) {
            off = (row * mat->col) + col;
            mat->a[off] = val++;
        }
    }
}

Matrix* sum(Matrix* mat1, Matrix* mat2){
    int row, col, k, l;
    Matrix *mat3;
    mat3 = allocateMemory(mat1->row, mat1->col);

    if((mat1->row == mat2->row)&&(mat1->col == mat2->col)){
        for (row = 0; row < mat1->row; row++){
            for (col = 0; col < mat1->col; col++){
                k = row * mat1->col + col;
                l = row * mat2->col + col;
                mat3->a[k] = mat1->a[k] + mat2->a[l];
            }
        }
        return mat3;
    }
}

int main(void){

   Matrix *m1 = allocateMemory(3,3);
   Matrix *m2 = allocateMemory(3,3);
   Matrix *m3 = allocateMemory(3,3);

    matrix_fill(m1);
    matrix_fill(m2);

    printf("\nMatrix 1\n");
    printMatrix(m1);

    printf("\nMatrix 2\n");
    printMatrix(m2);

    Matrix* sum(Matrix* m1, Matrix* m2);
    printf("\nSum of matrices m1 and m2\n");
    printMatrix(m3);

    return 0;
}

sum 函数应该返回两个给定矩阵之和的结果,但它不起作用,只是似乎返回 NULL 值。但我不明白为什么它不起作用。

【问题讨论】:

  • 旁白:为了避免int 溢出 huge 数组。考虑calloc(row * col, sizeof(*mat-&gt;a)); --> calloc((size_t)row * col, sizeof(*mat-&gt;a)); 之类的。

标签: c matrix memory-leaks dynamic-memory-allocation function-definition


【解决方案1】:

该函数具有未定义的行为和内存泄漏,因为在 if 语句中的条件满足时它不会返回任何内容

if((mat1->row == mat2->row)&&(mat1->col == mat2->col)){

将评估为假。

函数可以通过以下方式声明和定义

Matrix * sum( const Matrix *mat1, const Matrix *mat2 )
{
    Matrix *mat3 = NULL;

    if ( ( mat1->row == mat2->row ) && ( mat1->col == mat2->col ) )
    {
        mat3 = allocateMemory( mat1->row, mat1->col );
      
        if ( mat3 != NULL )
        {
            for ( int row = 0; row < mat1->row; row++ )
            {
                for ( int col = 0; col < mat1->col; col++ )
                {
                    int k = row * mat1->col + col;
                    mat3->a[k] = mat1->a[k] + mat2->a[k];
                }
            }
        }
    }

    return mat3;
}

函数main内this分配

Matrix *m3 = allocateMemory(3,3);

如果你像调用函数一样(也就是说,如果你会根据需要调用函数)是多余的,也会导致内存泄漏

m3 = sum( m1, m2 );

而这条记录在 main

Matrix* sum(Matrix* m1, Matrix* m2);

是一个函数声明。这不是函数调用。

你应该像上图那样写

Matrix *m3 = NULL;

//...

m3 = sum( m1, m2 );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 2013-04-06
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 2014-09-18
    相关资源
    最近更新 更多