首页 > 解决方案 > 你能解释一下下面代码中释放动态分配内存的代码在哪里以及如何出错吗?

问题描述

你能解释一下下面代码中释放动态分配内存的代码在哪里以及如何出错吗?

这是通过内存动态分配来初始化、打印和释放二维数组的代码。

#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;
}

如果出现问题,我该如何解决?

标签: cmallocfreedouble-pointer

解决方案


发布的代码是可以的,但请注意以下备注:

  • 它有点难以阅读:使用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;
}

推荐阅读