C - 在函数中分配矩阵

时间:2013-04-14 21:41:23

标签: c function matrix malloc

我正在尝试使用一个带有尺寸和三指针的函数来分配矩阵。我已经分配了一个int **(设置为NULL),我将其地址作为函数的参数传递。由于某种原因,这给了我一个mem访问冲突。

void allocateMatrix(int ***matrix, int row, int col)
{
    int i;
    if((*matrix = (int**)malloc(row * sizeof(int*))) == NULL)
    {
        perror("There has been an error");
        exit(EXIT_FAILURE);
    }
    for(i = 0; i < row; ++i)
    {
        if((*matrix[i] = (int*)malloc(col * sizeof(int))) == NULL)
        {
            perror("There has been an error");
            exit(EXIT_FAILURE);
        }
    }
}

/* main.c */

    int** matrix = NULL;
    allocateMatrix(&matrix, MATRIX_ROW, MATRIX_COL); //error

3 个答案:

答案 0 :(得分:3)

您需要更改

if((*matrix[i] = (int*)malloc(col * sizeof(int))) == NULL)

if(((*matrix)[i] = (int*)malloc(col * sizeof(int))) == NULL)
//  ^       ^

在使用数组下标之前,您需要取消引用matrix
*matrix[i]相当于*(matrix[i])

答案 1 :(得分:3)

这是运营商优先权的问题。在

if ((*matrix[i] = (int*)malloc( ... ))

默认优先级为*(matrix[i]),而您应使用(*matrix)[i]

我仍然建议将矩阵分配为连续数组,而不是指向数组的指针数组。

答案 2 :(得分:1)

我为gcc C11 / C99制作了一个解决方案程序,并根据链接提供了适当的分配功能:

http://c-faq.com/aryptr/dynmuldimary.html

http://c-faq.com/aryptr/ary2dfunc3.html

经过评论中的一些讨论后,很明显矩阵2被正确分配,它可以作为matrix2 [0]传递给这个函数fn(int row,int col,int array [col] [row])(数据在一维数组)与强制转换为(double(*)[])

//compile with gcc --std=c11 program.c
#include <stdio.h>
#include <stdlib.h>

#define MX 9
#define MY 14

void input_matrix(int row, int column, double matrix[row][column]);
void print_matrix(int row, int column, double matrix[row][column]);
double **alloc_matrix2(int row, int column);
double  *alloc_matrix3(int row, int column);
void    *alloc_matrix4(int row, int column);

int main()
{
    int i=MX, j=MY;
    printf("Generate input values and print matrices with functions fn(int w, int k, double matrix[w][k]) (in C99 and C11)\n");
    double matrix1[i][j];
    input_matrix(MX,MY,matrix1);
    printf("matrix static\n");
    print_matrix(MX,MY,matrix1);


    double **matrix2; //data of matrix2 is just matrix3                 
    matrix2=alloc_matrix2(MX,MY); 
    input_matrix(MX,MY,(double (*)[])(*matrix2));
    printf("matrix two times allocated one for pointers, the second for data (double (*)[])(m[0])\n");
    print_matrix(MX,MY,(double (*)[])(matrix2[0]));
    free(*matrix2);
    free(matrix2);


    double *matrix3=alloc_matrix3(MX,MY);
    input_matrix(MX,MY,(double (*)[])matrix3);
    printf("matrix allocated as two-dimensional array\n");
    print_matrix(MX,MY,(double (*)[])matrix3);
    free(matrix3);

    j=MY;
    double (*matrix4)[j];
    matrix4 = (double (*)[])alloc_matrix4(MX,MY);
    input_matrix(MX,MY,matrix4);
    printf("matrix allocated via pointer to array m = (double (*)[])malloc(MX * sizeof(*m))\n");
    print_matrix(MX,MY,matrix4);
    free(matrix4);
    printf("\nThe End!\n");
    return 0;
}

void input_matrix(int row, int column, double matrix[row][column]){
    for(int i=0; i<row; i++){
        for(int j=0; j<column; j++)
            matrix[i][j]=i+1;
    }
}

void print_matrix(int row, int column, double matrix[row][column]){
    for(int i=0; i<row; i++){
        for(int j=0; j<column; j++)
            printf("%.2lf ", matrix[i][j]);
        printf("\n");
    }
}

double **alloc_matrix2(int row, int column){
    double **matrix;
    matrix=malloc(row*sizeof(double*));
    matrix[0] = (double *)malloc(row*column*sizeof(double));
    for(int i = 1; i < row; i++)
        matrix[i] = matrix[0]+i*column;
    return matrix;
}

double *alloc_matrix3(int row, int column){
    double *matrix;
    matrix=malloc(row*column*sizeof(double));
    return matrix;
}

void *alloc_matrix4(int row, int column){
    double (*matrix)[column];
    matrix = (double (*)[])malloc(row*sizeof(*matrix));
    return matrix;
}