【问题标题】:Segmentation fault when using malloc for a 2D array [duplicate]将malloc用于二维数组时出现分段错误[重复]
【发布时间】:2020-01-29 02:50:50
【问题描述】:

我正在尝试打开一个包含两个矩阵的维度和值的文件。我想存储矩阵的维度,并使用从文件中读取的维度创建三个 2D int 数组(第三个是矩阵乘法的结果)。我正在传递一个指向参数的 int 指针的指针,因此 read_matrices 方法的参数是 int**(赋值要求)。

我在代码的 malloc 部分中不断遇到分段错误,但无法找出问题所在。它似乎适用于前两个矩阵,我可以在 array3 的 malloc 期间打印第一个 printf 语句,但在第二个 printf 语句之前出现分段错误。我认为循环中有问题,但我无法弄清楚,特别是因为它似乎适用于 array1 和 array2 的 malloc (除非整个事情都是错误的)。任何帮助表示赞赏。

编辑:编辑为仅包含代码中最重要的部分

void read_matrices(int **array1, int **array2, int **array3, int *m, int *n, int *p, char* file)
{

   /* Get the size of the matrices */
    fscanf(fp, "%d", m);
    fscanf(fp, "%d", n);
    fscanf(fp, "%d", p);

    /* Use malloc to allocate memory for matrices/arrays A, B, and C */
    array1 = (int**) malloc(*m * (sizeof(int*)));

    for (i = 0; i < *m; i++)
    {
        *(array1 + i) = (int*) malloc(*n * (sizeof(int)));
    }


    array2 = (int**) malloc(*n * (sizeof(int*)));

    for (i= 0; i < *n; i++)
    {
        *(array2 + i) = (int*) malloc(*p * (sizeof(int)));
    }


    array3 = (int** ) malloc(*m * (sizeof(int*)));

    for (i= 0; i < *m; i++)
    {
        *(array3 + i) = (int *) malloc(*p * (sizeof(int)));
        printf("Going through malloc3 loop\n");
    }
    printf("End of the third part of malloc\b");

    /* Close the stream */
    fclose(fp);
}

int main (int argc, char *argv[])
{
    /* Int pointers to three matrices */
    int *A, *B, *C;

    /* Int variables to store matrix dimensions */
    int m, n, p;

    /* Get the name of the file */
    char* filename = *(argv + 1);

   /* Read the matrices and fill matrices A and B */
    read_matrices(&A, &B, &C, &m, &n, &p, filename);

    /* Exit the system */
    return 0;
}

【问题讨论】:

标签: c multidimensional-array segmentation-fault malloc


【解决方案1】:

要为A[m][n]分配内存,可以使用指向指针的指针int **A;
分配的区别在于行和列。三个指针中的每一个都可以使用一个函数。
可以添加另一个函数来从文件中读取值。该函数将需要指针、行、列和文件指针。

#include <stdio.h>
#include <stdlib.h>
int get_size ( void)
{
    char input[100] = "";
    int size = 0;
    int result = 0;
    do {
        if ( fgets ( input, sizeof input, stdin)) {
            result = sscanf ( input, "%d", &size);
        }
        else {
            fprintf ( stderr, "fgets EOF\n");
            exit ( 0);
        }
    } while ( 1 != result || 0 >= size);
    return size;
}
int **free_matrice ( int **array, int row)
{
    for ( int i = 0; i < row; i++)
    {
        free ( array[i]);
    }
    free ( array);

    return NULL;
}
int **create_matrice ( int row, int col)
{
    int **array1 = NULL;
    /* Use malloc to allocate memory for matrices/arrays A, B, and C */
    if ( NULL == ( array1 = malloc ( sizeof(int*) * row))) {
        fprintf ( stderr, "problem malloc\n");
        exit ( 0);
    }
    for ( int i = 0; i < row; i++)
    {
        if ( NULL == ( array1[i] = malloc ( sizeof(int) * col))) {
            fprintf ( stderr, "problem malloc\n");
            exit ( 0);
        }
    }

    return array1;
}

int main (int argc, char *argv[])
{
    /* Int pointers to three matrices */
    int **A, **B, **C;

    /* Int variables to store matrix dimensions */
    int m = 0, n = 0, p = 0;

    printf ( "enter size for m\n");
    m = get_size ( );
    printf ( "enter size for n\n");
    n = get_size ( );
    printf ( "enter size for p\n");
    p = get_size ( );

    A = create_matrice ( m, n);
    B = create_matrice ( n, p);
    C = create_matrice ( m, p);

    A = free_matrice ( A, m);
    B = free_matrice ( B, n);
    C = free_matrice ( C, m);

    return 0;
}

【讨论】:

    猜你喜欢
    • 2015-04-13
    • 2018-09-22
    • 1970-01-01
    • 2020-12-28
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    • 2016-11-28
    相关资源
    最近更新 更多