动态2D阵列和内存免费

时间:2017-01-12 03:03:35

标签: c arrays dynamic-arrays

我有一个很大的麻烦,想弄清楚如何正确地创建一个2D动态数组,如何断言内存以及如何最终释放它。

我会告诉你部分代码,请告诉我我做错了什么。

我在main函数中声明动态数组并将其发送到BuildMatrix函数,该函数应该为数组断言所需的内存并填充它。

这就是我在数组上声明并将其发送到函数Build:

的方式
int row, column, i, j;
int **matrix; 
BuildMatrix(&matrix, row, column);

现在是BuildMatrix decleration:

void BuildMatrix(int*** matrix, int row, int column);

这就是我断言内存的方式(行和列具有用户选择的值)

matrix =malloc(row * sizeof(int *));
for (i = 0; i < row; i++)
matrix[i] =malloc(column * sizeof(int));

现在到目前为止一切正常,但是当我尝试释放内存时,我得到了断点错误

这是我用来释放内存的功能:

void ExitAndFree(int** matrix, int row) {
    int i;
    for (i = 0; i < row; i++) {
        free(matrix[i]);
    }
    free(matrix);
}

调试器告诉我错误是第一次免费(如果我删除第一个,第二个给出错误)

还有另外一个问题,但我现在想的很多,我稍后会问......:P

感谢您的帮助!!

P.S:如果你有关于指针和动态数组的任何好教程(我更喜欢2D +数组),我会非常感激。

2 个答案:

答案 0 :(得分:1)

我认为您不需要***指针,而BuildMatrix只能将**返回main()。这种设计更改会使您的程序更容易,因为使用***有时会很痛苦。

您也没有检查malloc()scanf()的返回值,这可能会导致未来出现问题,而且首先检查这些问题会更安全。我也建议你Don't cast result of malloc(),因为这在C中并不是真的需要。

如果您在@ flintlock回答的代码中出现free()错误,则代码中存在不一致之处:

您已声明:

void ExitAndFree(int** matrix, int row)

应该改为:

void ExitAndFree(int*** matrix, int row)

您的代码需要进行此更改,因为您在&matrix的main中调用了ExitAndFree(),因此在此函数中使用**matrix是不够的。同样,这是因为代码使用***,这使生活更加艰难。

您的代码似乎与here一起使用此更改。

通过这些建议,您还可以实现以下程序:

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

int **BuildMatrix(int row, int column);
void PrintAndFree(int **matrix, int row, int column);

int main(void) {
    int **matrix, row, column;

    printf("\nPlease enter number of rows:\n");
    if (scanf("%d", &row) != 1) {
        printf("Invalid rows.\n");
        exit(EXIT_FAILURE);
    } 

    printf("\nPlease enter number of columns:\n");
    if (scanf("%d", &column) != 1) {
        printf("Invalid columns.\n");
        exit(EXIT_FAILURE);
    }

    matrix = BuildMatrix(row, column);

    PrintAndFree(matrix, row, column);

    return 0;
}

int **BuildMatrix(int row, int column) {
    int **matrix, rows, cols;

    matrix = malloc(row * sizeof(*matrix));
    if (matrix == NULL) {
        printf("Cannot allocate %d rows for matrix.\n", row);
        exit(EXIT_FAILURE);
    }

    for (rows = 0; rows < row; rows++) {
        matrix[rows] = malloc(column * sizeof(*(matrix[rows])));
        if (matrix[rows] == NULL) {
            printf("Cannot allocate %d columns for row.\n", column);
            exit(EXIT_FAILURE);
        }
    }

    printf("\nPlease enter values to the matrix:\n");
    for (rows = 0; rows < row; rows++) {
        for (cols = 0; cols < column; cols++) {
            if (scanf("%d", &matrix[rows][cols]) != 1) {
                printf("Invalid value entered.\n");
                exit(EXIT_FAILURE);
            }
        }
    }

    return matrix;
}

void PrintAndFree(int **matrix, int row, int column) {
    int rows, cols;

    printf("\nYour matrix:\n");
    for (rows = 0; rows < row; rows++) {
        for (cols = 0; cols < column; cols++) {
             printf("%d ", matrix[rows][cols]);
        }
        free(matrix[rows]);
        matrix[rows] = NULL;
        printf("\n");
    }

    free(matrix);
    matrix = NULL;
}

答案 1 :(得分:0)

这是一个复制和粘贴准备好的示例......

#include <stdlib.h>

void BuildMatrix(int*** matrix, int row, int column) {
    *matrix = malloc(row * sizeof(int *));
    int i;
    for (i = 0; i < row; i++)
        (*matrix)[i] = malloc(column * sizeof(int));
}

void ExitAndFree(int** matrix, int row) {
    int i;
    for (i = 0; i < row; i++) {
        free(matrix[i]);
    }
    free(matrix);
}

int main()
{
    int row = 10, column = 10;
    int **matrix;
    BuildMatrix(&matrix, row, column);
    ExitAndFree(matrix, row);

    return 0;
}

但是我强烈建议不要直接使用原始指针和矩阵的单独变量。一个更好的解决方案软件工程明智的做法是使用现有的矩阵库,或用C语言编写自己的矩阵库和结构...

相关问题