我可以不断地malloc内存而不释放内存吗?

标签 c memory memory-management linked-list malloc

这是我的代码:

struct movie {
    char movie_name[30];
    float score;
    struct movie *next;
};

typedef struct movie *p_movie;
void print_all(struct movie *head);

int main() {
    p_movie head = NULL;
    p_movie new_node = malloc(sizeof(struct movie));
    if (new_node == NULL) {
        exit(1);
    }
    strcpy(new_node->movie_name, "Avatar");
    new_node->score = 9.5f;
    new_node->next = NULL;
    if (head == NULL)
        head = new_node;
    print_all(head);
    new_node = malloc(sizeof(struct movie));
    if (new_node == NULL) {
        exit(1);
    }
    strcpy(new_node->movie_name, "Aladdin");
    new_node->score = 8.0f;
    new_node->next = NULL;
    p_movie temp = head;
    head = new_node;
    new_node->next = temp;

    print_all(head);
}

void print_all(struct movie *head) {
    printf("---------------------\n");
    printf("Head address = %zd\n", (size_t)head);
    struct movie* search = head;
    while (search != NULL) {
        printf("%zd \"%s\" %f %zd\n", (size_t)search, search->movie_name,
               search->score, (size_t)search->next);
        search = search->next;
    }
}

我很困惑,因为malloc:它运行良好,但我不知道为什么这段代码运行良好。

我的问题是关于 new_node 的:首先,我为 new_node 分配内存,并且不会释放它并再次分配内存。

那么,第一段内存(阿凡达)发生了什么?那是被删了吗?或者保存在某个地方..?

最佳答案

Can I constantly malloc memory without free?

理论上是的。实际上,直到内存耗尽为止。

first, I allocate memory to new_node.

And I didnt' free that and allocate memory again.

then, what's going on first memory (Avatar)?

Is that deleted? or saved somewhere..?

在你的具体例子中:如果head == NULL,它(显然)被保存。这里,“it”指的是指向该内存的指针。

如果head不是NULL(这在那个地方是不可能的),你就会发生内存泄漏:你已经分配了不能再引用的内存。

一般来说,在 malloc() 之后,您可以根据需要保留该内存。这包括保留它直到程序结束。但是,如果不保存指向该内存的指针,则无法对其执行任何操作:既不使用也不释放它。在这种情况下,您的程序需要的内存比应有的多,如果这种情况经常发生,它最终会不必要地阻塞您的内存。

但是,就您而言,您将此 new_node 保存在 head 中,因此它不会丢失。完成此操作后,您可以将 new_node (这只是一个用于保存指针的变量!)用于其他目的。

我建议将新节点的分配移至单独的函数:

p_movie alloc_movie(char * name, float score)
{
    p_movie new_node = malloc(sizeof(struct movie));
    if (new_node == NULL) {
        return NULL; // let the caller handle that
    }
    strcpy(new_node->movie_name, name);
    new_node->score = score;
    new_node->next = NULL;
    return new_node;
}

并使用它

int main()
{
    p_movie head = NULL;
    p_movie new_node = alloc_movie("Avatar", 9.5f);
    if (new_node == NULL)
    {
        exit(1);
    }
    // if (head == NULL) is unnecessary, as we can be sure it is null
    head = new_node;
    print_all(head);

    new_node = alloc_movie("Aladdin", 8.0f);
    if (new_node == NULL)
    {
        exit(1);
    }

    // Put the new node as the head, let the old head then be the next item.
    p_movie temp = head;
    head = new_node;
    new_node->next = temp;

    print_all(head);
}

或更短:

p_movie alloc_movie(char * name, float score, p_movie next)
{
    p_movie new_node = malloc(sizeof(struct movie));
    if (new_node == NULL) {
        return NULL; // let the caller handle that
    }
    strcpy(new_node->movie_name, name);
    new_node->score = score;
    new_node->next = next;
    return new_node;
}

int main()
{
    p_movie new_node = alloc_movie("Avatar", 9.5f, NULL);
    if (new_node == NULL)
    {
        exit(1);
    }
    p_movie head = new_node;
    print_all(head);

    // Create a new node, with the old head as its "next".
    new_node = alloc_movie("Aladdin", 8.0f, head);
    if (new_node == NULL)
    {
        exit(1);
    }

    // Overwrite the head, as its old value is already saved in the "Aladdin" node.
    head = new_node;
    print_all(head);
}

关于我可以不断地malloc内存而不释放内存吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61475943/

相关文章:

c++ - 如何在 Lua 和 C++ 中正确清理类?

c - 如何将随机数放入int数组C中

C如何判断一个char**占用多少内存

c++ - 推送 Lua 表

c++ - 使用多个堆进行内存管理有什么好处吗?

performance - 了解缓存/RAM 访问延迟的微基准

c - C 中的输入和输出

r - 使用具有 gmp 和机器限制的大整数

python - Python 在切片列表时是否复制对对象的引用?

c - C中数据类型的大小及其在内存中的大小