[英]Dynamic 2D array and memory free
我在弄清楚如何正确创建二维动态数组、如何断言内存以及最终如何释放它时遇到了很大的麻烦。
我将向您展示我的部分代码,请告诉我我做错了什么。
我在 main 函数中声明动态数组并将其发送到 BuildMatrix 函数,该函数应该将所需的内存断言到数组并填充它。
这就是我在数组上声明并将其发送到函数 Build 的方式:
int row, column, i, j;
int **matrix;
BuildMatrix(&matrix, row, column);
现在是 BuildMatrix 声明:
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
谢谢你的帮助!!
PS:如果你有关于指针和动态数组的任何好的教程(我更喜欢 2D+ 数组),我会非常感激。
我不认为您在这里需要***
指针,而是BuildMatrix
可以将**
返回到main()
。 这种设计更改将使您的程序更容易,因为有时使用***
可能会很痛苦。
您也没有检查malloc()
和scanf()
的返回值,这可能会导致未来的问题,首先检查它们更安全。 我还建议您不要强制转换 malloc() 的结果,因为这在 C 中并不是真正需要的。
由于您在@flintlock's answer 上发布的代码中的free()
错误,您的代码存在不一致之处:
您已声明:
void ExitAndFree(int** matrix, int row)
当这应该是:
void ExitAndFree(int*** matrix, int row)
在您的代码中需要进行此更改,因为您在 main 中为ExitAndFree()
调用&matrix
,因此在此函数中使用**matrix
还不够好。 同样,这是因为代码使用了***
,这让生活变得更加艰难。
您的代码似乎在这里可以使用此更改。
#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;
}
这是一个复制和粘贴准备好的示例...
#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 语言编写自己的矩阵库,并使用 struct...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.