动态分配矩阵的功能

CSD*_*ude 3 c malloc pointers dynamic matrix

我想创建一个函数来分配(带malloc/ calloc)声明为双指针的矩阵.我理解双指针矩阵如何工作以及如何分配它malloc,但当我传递我的矩阵(声明main()并初始化NULL)时,我的程序崩溃了.我想错误与我的allocMatrix()功能有关,因为如果我在main所有工作中顺利分配矩阵.谢谢 :-)

主要:

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

#define ROW 5
#define COL 5

int main(void) {

    int i,j, ret;
    int nRow, nCol;
    int **mat=NULL; // double pointer matrix

    nRow = 0;
    nCol = 0;

    //Insert n of row and columns
    printf("Insert n of rows and columns:\n");
    scanf("%d %d", &nRow, &nCol);

    //Functions to allocate matrix
    ret=allocMatrix(mat, nRow, nCol);
    printf("Return value: %d\n",ret);

    /*
    this code works perfect!
    mat= malloc(nRow * sizeof(int));
    i=0;
    while( i < nRow)
    {
        mat[i]=malloc(nCol * sizeof(int));
        i++;
    }
    */

    //Get Values from stdin
    i=0;
    while( i < nRow)
    {
        j=0;
        while (j < nCol)
        {
            printf("Insert value pos[%d,%d]:\n", i, j);
            scanf("%d", &mat[i][j]);
            j++;
        }
        i++;
    }

    //Print values
    i=0;
    while (i < nRow)
    {
        j=0;
        while( j < nCol)
        {
            printf("Value pos[%d,%d] is: %d \n", i, j, mat[i][j]);
            j++;
        }
        i++;
    }


    system("pause");
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

allocateMatrix 功能:

int allocMatrix(int **matrix, int nRow, int nCol)
{
    int i;
    int ext_status;

    //Classic allocation method for matrix
    matrix= malloc( nRow * sizeof(int));

    if ( matrix != NULL)
    {
        i=0;
        while (i < nRow)
        {
            matrix[i]= malloc(nCol * sizeof(int));

            if( matrix[i] != NULL)
                ext_status= 1;
            else
                ext_status= 0;
            i++;
        }
    }
    else
        ext_status= 0;

    return ext_status;
 }
Run Code Online (Sandbox Code Playgroud)

Lun*_*din 12

切勿使用指针指针来分配多维数组.它是广泛传播但是错误和不正确的做法.这样做不会给你一个真正的2D数组,并且由于堆碎片会导致代码变慢.它还使代码更难以编写,读取和维护,从而增加了内存泄漏的可能性.

相反,在相邻的存储单元中正确分配2D数组,如下所示:

int x;
int y;

// store some values in x and y here

int(*matrix)[y] = malloc (sizeof(int[x][y]));

if(matrix == NULL)
{
  // error handling here
}

matrix[i][j] = something; // do something with the matrix

free(matrix);
Run Code Online (Sandbox Code Playgroud)

如果你坚持将这段代码保存在一个函数中,那么它将是:

void* allocMatrix (int nRow, int nCol)
{
  return malloc (sizeof(int[nRow][nCol]));
}

int(*matrix)[y] = allocMatrix(x, y);
Run Code Online (Sandbox Code Playgroud)

编辑:代码和数组指针的解释.

在该行中int(*matrix)[y] = malloc (sizeof(int[x][y]));,sizeof(int[x][y])它非常直接,它只是尺寸为x*y的二维2D数组的大小.它使用了C99标准中的可变长度数组的概念,它允许在运行时指定数组维度.

一个数组指针在C有些特殊类型,它是能够指向整个阵列,而不是仅仅在阵列的第一项,作为一个正常的指针都行.与常规指针不同,数组指针知道数组的大小.

数组指针被写为type(*name)[size],因此例如,指向5个int的数组的数组指针将被写为int(*arr_ptr)[5] = &the_array;.

当访问指向的内容时,数组指针的行为与任何指针一样,您可以使用它来访问它的内容*.所以*arr_ptr给出指向的数组,并(*arr_ptr)[0]给出该数组的第一项.

对于多维数组,适用相同的规则.给定数组int arr[x][y],将指向此类型的数组指针int(*arr_ptr)[x][y] = &arr;.访问内容*arr_ptr将为您提供一个二维数组,相当于一个数组数组.(*arr_ptr)[0]因此将给出数组数组中的第一个数组.在表达式中使用时,任何数组名称的通常规则是它"衰减"为指向第一个元素的指针.这同样适用,(*arr_ptr)[0]因此也将与指向第一个数组中第一个元素的指针相同.而且(*arr_ptr)[0][0]会给第一个数组的第一个元素.

现在这个语法(*arr_ptr)[0][0]看起来有点难以阅读; 为了获得2D数组的第一项,我们只是在写作时使用arr[0][0].因此,在声明数组指针时,有一个方便的技巧.而不是声明完整和正确的数组指针:int(*matrix)[x][y],一个指向维数x*y的二维数组的数组指针,我们将其声明为int(*matrix)[y],这是一个指向维度为y的一维数组的数组指针.它将指向2D数组中的第一个项目,即一个大小为y的一维数组.我们知道2D数组包含x个这样的项目.

而且由于这个技巧,我们现在能够使用与访问2D数组时相同语法的数组指针,即matrix[i][j],而不是难以阅读(*matrix)[i][j].


Sou*_*osh 0

这里的问题是,您将mat自身传递给分配器函数,该函数在返回时无法保留函数分配的内存,因为C函数参数传递使用按值传递。

您需要传递一个指针mat并进行相应的分配。