Malloc是否免费的先前分配的内存,如果没有,以下练习问题解决方案到底有什么问题?

发布于 2025-02-04 07:13:36 字数 1355 浏览 5 评论 0原文

我是编程的新手,并且正在尝试在C中遇到一些练习问题。我遇到了以下提示:

/*
Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

 * Return an array of arrays.
 * The sizes of the arrays are returned as *columnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */

我第一次尝试使用二维阵列完成了问题,然后尝试使用malloc(AS AS)在问题中指定)并有点卡住。我查看了一些解决方案,发现一种与我的方法有些匹配的解决方案:

// Source: https://github.com/vli02/leetcode/blob/master/118.%20Pascal's%20Triangle.c
int** generate(int numRows, int** columnSizes) {
    int i, j;
    int *buff, **p;
    p = malloc(numRows * sizeof(int *));
    *columnSizes = malloc(numRows * sizeof(int));
    //assert(p && *columnSizes);
    for (i = 1; i <= numRows; i ++) {
        buff = malloc(i * sizeof(int));
        //assert(buff);
        p[i - 1] = buff;
        (*columnSizes)[i - 1] = i;
        buff[0] = 1;
        for (j = 1; j < i - 1; j ++) {
            buff[j] = p[i - 2][j - 1] + p[i - 2][j];
        }
        buff[i - 1] = 1;
    }
    return p;
}

我只想问的是,如果通过每个循环更改对Buff指针的相应地址,此解决方案是如何正确的,但是每个循环中分配的内存块永不释放?这不会导致内存泄漏吗?据我所知,Malloc不会释放以前分配的内存。我刚刚开始在C中进行编码,因此我不确定我应该知道的引擎盖下是否正在发生某些事情。任何帮助将不胜感激。

I'm new to programming and am trying to do some practice problems in C. I've come across the following prompt:

/*
Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

 * Return an array of arrays.
 * The sizes of the arrays are returned as *columnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */

I finished the problem using a two-dimensional array as a first attempt and then tried doing it by using malloc (as specified in the problem) and got a bit stuck. I looked over some solutions and found one that somewhat matched my approach:

// Source: https://github.com/vli02/leetcode/blob/master/118.%20Pascal's%20Triangle.c
int** generate(int numRows, int** columnSizes) {
    int i, j;
    int *buff, **p;
    p = malloc(numRows * sizeof(int *));
    *columnSizes = malloc(numRows * sizeof(int));
    //assert(p && *columnSizes);
    for (i = 1; i <= numRows; i ++) {
        buff = malloc(i * sizeof(int));
        //assert(buff);
        p[i - 1] = buff;
        (*columnSizes)[i - 1] = i;
        buff[0] = 1;
        for (j = 1; j < i - 1; j ++) {
            buff[j] = p[i - 2][j - 1] + p[i - 2][j];
        }
        buff[i - 1] = 1;
    }
    return p;
}

All I want to ask is, how is this solution correct if the corresponding address to the buff pointer is changed through every loop, but the blocks of memory allocated in each loop are never freed? Wouldn't this cause a memory leak? As far as I've seen, malloc will not free the previously allocated memory. I've just begun coding in C, so I'm not sure if something is happening under the hood that I should know about. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文