简体   繁体   English

C-矩阵乘法分割错误

[英]C - matrix multiplication segmentation fault

I get a segmentation fault when running this code. 运行此代码时出现分段错误。 Anyone know why? 有人知道为什么吗? Thanks. 谢谢。

#include <stdio.h>

int main()
{
    double **m1, **m2, **mr;
    int m1_rows, m1_cols, m2_rows, m2_cols, mr_rows, mr_cols;
    int i, j, k;

    printf("Enter number of rows for matrix 1: ");
    scanf("%d", &m1_rows);

    printf("Enter number of columns for matrix 1: ");
    scanf("%d", &m1_cols);

    printf("Enter number of rows for matrix 2: ");
    scanf("%d", &m2_rows);

    printf("Enter number of columns for matrix 2: ");
    scanf("%d", &m2_cols);

    //allocate memory for matrix 1 m1
    m1 = (double **) calloc(m1_rows, sizeof(double *));
    for (i = 0; i < m1_rows; i++) {
        m1[i] = (double *) calloc(m1_cols, sizeof(double));
    }

    //allocate memory for matrix 2 m2
    m2 = (double **) calloc(m2_rows, sizeof(double *));
    for (i = 0; i < m2_rows; i++) {
        m2[i] = (double *) calloc(m2_cols, sizeof(double));
    }

    //allocate memory for sum matrix mr
    mr = (double **) calloc(mr_rows, sizeof(double *));
    for (i = 0; i < mr_rows; i++) {
        mr[i] = (double *) calloc(mr_cols, sizeof(double));
    }

    //assign  mr_rows and mr_cols
    mr_rows = m1_rows;
    mr_cols = m2_cols;

    //initialize product matrix
    for (i = 0; i < m1_rows; i++) {
        for (j = 0; j < m2_cols; j++) {
            mr[i][j] = 0;
        }
    }

    //perform matrix multiplication
    for (i = 0; i < m1_rows; i++) {
        for (j = 0; j < m2_cols; j++) {
            mr[i][j] = 0;
            for (k = 0; k < m1_cols; k++) {
                mr[i][j] += m1[i][k] * m2[k][j];
            }
        }
    }

    //print result
    for (i = 0; i < mr_rows; i++) {
        for (j = 0; j < mr_cols; j++) {
            printf("%f\t", mr[i][j]);
        }
    }

    //free memory m1
    for (i = 0; i < m1_rows; i++); {
        free(m1[i]);
    }
    free(m1);

    //free memory m2
    for (i = 0; i < m2_rows; i++); {
        free(m2[i]);
    }
    free(m2);

    //free memory mr
    for (i = 0; i < mr_rows; i++); {
        free(mr[i]);
    }
    free(mr);

    return 0;
}

I ran using valgrind valgrind --tool=memcheck a.out for more info on the segmentation fault, but the result was over 30000 errors so it didn't print them out. 我使用valgrind valgrind --tool=memcheck a.out来获取有关分段错误的更多信息,但结果超过30000个错误,因此没有将其打印出来。

You are not assigning mr_rows and mr_cols . 您没有分配mr_rowsmr_cols They need to be set like this: 他们需要这样设置:

mr_rows = m1_rows;
mr_cols = m2_cols;

This line is no good: 这行是不好的:

mr[i][j] += m1[i][k] * m2[k][j];

That will be accessing elements out of bounds, not least because k is not initialized. 那将超出范围访问元素,尤其是因为未初始化k You need that line of code inside three nested for loops. 您需要在三个嵌套的for循环内的那行代码。 Indeed, you may as well roll the zeroising code into this too. 确实,您也可以将调零代码加入其中。

for(i=0; i<m1_rows; i++){
    for(j=0; j<m2_cols; j++){
        mr[i][j] = 0;
        for(k=0; k<m1_cols; k++){
            mr[i][j] += m1[i][k]*m2[k][j];
        }
    }
}

Also, all your memory freeing loops are wrong. 同样,您所有的内存释放循环都是错误的。 Instead of 代替

for(i=0; i<m1_rows; i++);{
    free(m1[i]);
}
free(m1);

it should read 它应该读

for(i=0; i<m1_rows; i++){
    free(m1[i]);
}
free(m1);

That stray semicolon was killing you. 那个流浪的分号正在杀死你。

You also need to perform a check that the number of columns in m1 equals the number of rows in m2 , ie test that m1_cols == m2_rows . 您还需要执行检查以确保m1的列数等于m2的行数,即测试m1_cols == m2_rows

One final point. 最后一点。 You duplicate your code horribly here. 您在这里可怕地复制了代码。 Don't have three identical for loops to allocate a matrix and three identical for loops to deallocate. 没有三个相同的for循环分配矩阵,没有三个相同的for循环分配矩阵。 Extract those operations into helper functions which can be called from main . 将这些操作提取到可以从main调用的帮助器函数中。

That's all that I can find! 这就是我所能找到的!

You are not assigning any values to mr_rows and mr_cols anywhere. 您没有在任何地方为mr_rows and mr_cols分配任何值。 So they will have junk values and you use them to to allocate memory by callin calloc(). 因此它们将具有垃圾值,您可以使用它们通过调用calloc().来分配内存calloc().

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM