c - 你能解释一下下面代码中释放动态分配内存的代码在哪里以及如何出错吗?
问题描述
你能解释一下下面代码中释放动态分配内存的代码在哪里以及如何出错吗?
这是通过内存动态分配来初始化、打印和释放二维数组的代码。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
int idx, row, col, x, y;
int **pptr = (int **)malloc(sizeof(int *) * 8);
if (NULL == pptr) {
printf("ERROR: malloc failed.\n");
exit(1);
}
for (idx = 0; idx < 8; idx++) {
*(pptr + idx) = (int*)malloc(sizeof(int) * 6);
if (NULL == *(pptr + idx)) {
printf("ERROR: malloc failed.\n");
exit(1);
}
}
for (row = 0; row < 8; row++) {
for (col = 0; col < 6; col++) {
*(*(pptr + row) + col) = 6 * row + col;
}
}
for (row = 0; row < 8; row++) {
for (col = 0; col < 6; col++) {
printf("%3d", *(*(pptr + row) + col));
}
printf("\n");
}
for (idx = 0; idx < 8; idx++) {
free(*(pptr + idx));
if (NULL != *(pptr + idx)) {
*(pptr + idx) = NULL;
}
}
free(pptr);
if (NULL != pptr) {
pptr = NULL;
}
return 0;
}
如果出现问题,我该如何解决?
解决方案
发布的代码是可以的,但请注意以下备注:
- 它有点难以阅读:使用
pptr[idx]
而不是*(pptr + idx)
会提高可读性而不会产生任何后果,因为两种语法是等效的。 - 它有多余的测试:没有必要测试一个指针是否
NULL
在你将它设置为NULL
. - 在 C 中强制转换 of 的返回值
malloc()
是没有用的(尽管在 C++ 中它是强制性的,malloc()
无论如何你都不应该使用)。 - 变量
x
并且y
未使用。 - 如果发生错误,您不会释放分配的内存,这并不是真正的错误,因为操作系统将在退出时回收所有进程的内存,您会立即请求。请注意,您可以使用与函数相同的效果来
return 1;
代替。exit(1)
main()
- 定义矩阵中的列数和行数将有助于增强一致性并提高可读性。
- 您可以在分配和释放循环中使用
row
而不是。idx
这是修改后的版本:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define NROWS 8
#define NCOLS 6
int main() {
int row, col;
int **pptr = malloc(sizeof(*pptr) * ROWS);
if (!pptr) {
fprintf(stderr, "ERROR: malloc failed.\n");
exit(1);
}
for (row = 0; row < NROWS; row++) {
pptr[row] = malloc(sizeof(*pptr[row]) * NCOLS);
if (!pptr[row]) {
fprintf(stderr, "ERROR: malloc failed.\n");
exit(1);
}
}
for (row = 0; row < NROWS; row++) {
for (col = 0; col < NCOLS; col++) {
pptr[row][col] = NCOLS * row + col;
}
}
for (row = 0; row < NROWS; row++) {
for (col = 0; col < NCOLS; col++) {
printf("%3d", pptr[row][col]);
}
printf("\n");
}
for (row = 0; row < NROWS; row++) {
free(pptr[row]);
pptr[row] = NULL;
}
free(pptr);
pptr = NULL;
return 0;
}
您没有分配真正的2D 矩阵。这是另一种方法:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define NROWS 8
#define NCOLS 6
int main() {
int row, col;
/* allocate a 2D matrix: an array or arrays of NCOLS ints */
int (*pptr)[NCOLS] = malloc(sizeof(*pptr) * ROWS);
if (!pptr) {
fprintf(stderr, "ERROR: malloc failed.\n");
exit(1);
}
for (row = 0; row < NROWS; row++) {
for (col = 0; col < NCOLS; col++) {
pptr[row][col] = NCOLS * row + col;
}
}
for (row = 0; row < NROWS; row++) {
for (col = 0; col < NCOLS; col++) {
printf("%3d", pptr[row][col]);
}
printf("\n");
}
free(pptr);
pptr = NULL;
return 0;
}
推荐阅读
- python - 没有 ORM 的烧瓶纯 SQL
- mysql - MySQL 赋予用户权限而不拥有它们
- reactjs - 如何从组件访问父组件的状态
- javascript - MongoDB 并发问题 - FindOne,然后 FindOneAndUpdate 原子性
- python - 正则表达式错误:在位置 0 没有可重复的内容
- angular - 为 SAM 配置 CORS。API 适用于邮递员,但不适用于 Angular
- reactjs - 使用 yarn dlx create-react-app 创建 React 应用程序后,第一次按 yarn start 启动它时,出现错误
- python - on_guild_join discord python 为什么这不起作用?
- javascript - 在 Emotion 中合并单个 CSS 属性
- c - qsort:转换比较器函数本身还是比较器函数体中的参数?