【问题标题】:Creating dynamic two dimensional array in C在C中创建动态二维数组
【发布时间】:2020-05-20 08:33:30
【问题描述】:

我想写一个计算矩阵乘法的程序。

但是,内存分配似乎存在问题。对于 m

这是我的代码:

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

typedef double *Vector;
typedef Vector *Matrix;

//Initializes a matrix of given size
Matrix mat_alloc(int m, int n){
    Matrix mat = NULL;
    mat = (Matrix)malloc(sizeof(m * sizeof(Vector)));
    if(mat == NULL){
        printf("Error: Not Enough Memory!\n");
        return NULL;
    }
    for(int i = 0; i < m; i++){
        mat[i] = (Vector)malloc(n * sizeof(double));
        if(mat[i] == NULL){
            printf("Error: Not Enough Memory!\n");
            return NULL;
        }
    }
    return mat;
}

Matrix mat_mult(Matrix A, Matrix B, int m, int n, int k){
    Matrix C = mat_alloc(m, k);
    for(int i = 0; i < m; i++){
        for(int j = 0; j < k; j++){
            C[i][j] = 0;
            for(int l = 0; l < n; l++){
                C[i][j] += A[i][l] * B[l][j];
            }
        }
    }

    return C;
}

void print_matrix(Matrix mat, int m, int n){
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            printf("%.2lf ", mat[i][j]);
        }
        printf("\n");
    }
}

void read_matrix(Matrix mat, int m, int n){
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            printf("(%d,%d) = ", i+1, j+1);
            scanf("%lf", &mat[i][j]);
        }
    }
}

void free_matrix(Matrix mat, int m){
    for(int i = 0; i < m; i++){
        free(mat[i]);
    }
    free(mat);
}

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

    int m = 0;
    int n = 0;
    int k = 0;

    printf("Dimensions of A (m x n):\n");
    printf("m = ");
    scanf("%d", &m);
    printf("n = ");
    scanf("%d", &n);

    printf("Dimensions of B (n = %d x k):\n", n);
    printf("k = ");
    scanf("%d", &k);

    printf("Your input: m = %d, n = %d, k = %d\n", m, n, k);

    Matrix A = NULL;
    Matrix B = NULL;
    Matrix C = NULL;

    A = mat_alloc(m, n);
    B = mat_alloc(n, k);

    printf("Enter Values for A!\n");
    read_matrix(A, m, n);

    printf("Enter Values for B!\n");
    read_matrix(B, n, k);

    printf("A = \n");
    print_matrix(A, m, n);

    printf("\nB = \n");
    print_matrix(B, n, k);

    C = mat_mult(A, B, n, m, k);
    printf("\nC = \n");
    print_matrix(C, m, k);

    free_matrix(A, m);
    free_matrix(B, n);
    free_matrix(C, m);

    return 0;
}

提前谢谢你。

【问题讨论】:

  • 所有这些代码都是重现问题所必需的吗?看起来您至少可以跳过mat_mult 并硬编码有问题的值,而不是使用scanf。花点时间创建一个minimal reproducible example。还有don't cast malloc
  • mat = (Matrix)malloc(sizeof(m * sizeof(Vector))); 更改为 mat = malloc(m * sizeof(*mat)); 并避免头痛
  • 在 typedef 后面隐藏指针是万恶之源。除了您发明的这种外来指针(指向指针)语言,您还可以使用动态分配的二维数组。 stackoverflow.com/questions/42094465/…
  • 建议:使用VLS的简单分配。 A[m][n];B[n][k]C[m][k]
  • @TruthSeeker 什么是 VLS?你是说C的变长数组VLA

标签: c matrix segmentation-fault malloc


【解决方案1】:

这里是:

mat = (Matrix)malloc(sizeof(m * sizeof(Vector)));

应该是

mat = (Matrix)malloc(m * sizeof(Vector));

或者更好

mat = malloc(sizeof *mat * m);

您的sizeof 太多了,因此您获得的不是指针数组的所需大小,而是一个常量(size_t 的大小)。

【讨论】:

    猜你喜欢
    • 2013-02-22
    • 2011-08-14
    • 2017-06-01
    • 2019-04-19
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    相关资源
    最近更新 更多