C:释放每个 malloc 还是仅释放主要的?

发布于 2024-11-07 09:58:53 字数 978 浏览 0 评论 0原文

我有一个指针 ***resultSet,我将其作为参数传递给 SQL 函数,该函数从数据库读取未知数量的数据。数据的实际处理发生在函数之外。

当清理这个指针构造时,我是否只释放主指针一次并释放所有后续分配,或者我是否必须释放我创建的每个 malloc?

while((row = mysql_fetch_row(result))) {

                // allocating the rows pointer
                resultSet = malloc(sizeof(void)*(int)mysql_num_rows);
                (*rows)++;

                for (i=0 ; i < mysql_num_fields(result); i++)               
                {
                // allocating the fields pointer
                *(resultSet+i) = malloc(sizeof(void)*(int)mysql_num_fields);

                // allocating the character pointer
                **resultSet = malloc(sizeof(char)*strlen(row[i])+1);
                (*fields)++;

                snprintf(**resultSet, strlen(row[i])+1, "%s", row[i]);
                printf("%s\t",**resultSet);
                if (i==6)
                {
                printf("\t %s\n",**resultSet);
                }
                }
    }

I have a pointer ***resultSet that I pass as parameter to my SQL function which reads an unknown amount of data from the DB. The actual handling of the data happens outside the function.

When cleaning up this pointer construct, do I just free the main pointer once and free finds all the subsequent allocations or do I have to free up every malloc I created?

while((row = mysql_fetch_row(result))) {

                // allocating the rows pointer
                resultSet = malloc(sizeof(void)*(int)mysql_num_rows);
                (*rows)++;

                for (i=0 ; i < mysql_num_fields(result); i++)               
                {
                // allocating the fields pointer
                *(resultSet+i) = malloc(sizeof(void)*(int)mysql_num_fields);

                // allocating the character pointer
                **resultSet = malloc(sizeof(char)*strlen(row[i])+1);
                (*fields)++;

                snprintf(**resultSet, strlen(row[i])+1, "%s", row[i]);
                printf("%s\t",**resultSet);
                if (i==6)
                {
                printf("\t %s\n",**resultSet);
                }
                }
    }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月静好 2024-11-14 09:58:53

作为一般规则,对于每个 malloc() 都有一个 free()

As a general rule, for every malloc() you have a free().

昔日梦未散 2024-11-14 09:58:53

free() 释放 ptr 指向的内存空间,该空间必须由之前调用 malloc() 返回, calloc()realloc()

它的作用正如它所说的那样——释放指向的内存。它不查看内存中存储的内容;而是查看内存中存储的内容。它不知道任何其他分配的内存。它只是释放你告诉它的东西。因此,正如其他人所说,如果您想释放分配的所有内存,则必须释放所有malloc的内容。

(也许您正在考虑 C++ 更复杂的行为,当您销毁一个对象时,它的成员也会被销毁?)

free() frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc().

It does exactly what it says - frees the memory pointed to. It doesn't look at what's stored in that memory; it has no idea about any other allocated memory. It just frees what you tell it to. So, as others have said, if you want to free all the memory you allocated, you have to free everything you malloced.

(Perhaps you were thinking of the more sophisticated behavior of C++, where when you destroy an object, its members are also destroyed?)

去了角落 2024-11-14 09:58:53

您必须为您声明的每个 malloc() 调用 free() 以防止内存泄漏。

you have to call free() for every malloc() you stated to prevent a memory leak.

黎歌 2024-11-14 09:58:53

您基本上需要反向工作并释放每个分配。

You will need to basically work in reverse and free each allocation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文