何时释放 C 代码中的内存?

发布于 2024-08-17 13:19:51 字数 283 浏览 8 评论 0原文

例如,当我在 while 循环外部分配内存时,可以在其中释放内存吗? 这两个代码等效吗?

int* memory = NULL;
memory = malloc(sizeof(int));
if (memory != NULL)
{
  memory=10;
  free(memory);
}


int* memory = NULL;
memory = malloc(sizeof(int));
if (memory != NULL)
{
  memory=10;
}
free(memory);

When I alloc memory outside a while loop for example, is it okay to free it inside it ?
Are these two codes equivalent ?

int* memory = NULL;
memory = malloc(sizeof(int));
if (memory != NULL)
{
  memory=10;
  free(memory);
}


int* memory = NULL;
memory = malloc(sizeof(int));
if (memory != NULL)
{
  memory=10;
}
free(memory);

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

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

发布评论

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

评论(6

一张白纸 2024-08-24 13:19:52

是的,它们是等价的。如果分配不成功,您不必调用 free()
请注意,memory 是指向 int 的指针,并且您必须取消引用它才能将某些内容分配给它的内存块;

int* memory = NULL;
memory = malloc(sizeof(int));
if (memory)
    *memory=10;
free(memory);
memory = NULL;

Yes, they are equivalent. You do not have to call free() if allocation did not succeed.
Pay attention, that memory is pointer to int and you have to dereference it to assign something to its memory block;

int* memory = NULL;
memory = malloc(sizeof(int));
if (memory)
    *memory=10;
free(memory);
memory = NULL;
2024-08-24 13:19:52

如果您确定只释放它一次,那也没关系。

一个好主意是在释放指针时始终将其设置为 =NULL,那么如果它再次被释放则无关紧要

Doesn't matter IF you are sure you only free it once.

A good idea is to always set a pointer =NULL when you free it, then if it gets free'ed again it won't matter

人生百味 2024-08-24 13:19:52
int* memory = NULL;
memory = malloc(sizeof(int));
if (memory != NULL)
{
  memory=10;
  free(memory);
}

这会崩溃。您将指针设置为内存位置 10,然后要求系统释放内存。即使在虚拟地址空间的疯狂世界中,您之前分配的某些内存也极不可能恰好从 10(十六进制的 0xA)开始。此外,如果 malloc 失败,则没有分配内存,因此不需要释放它。

int* memory = NULL;
memory = malloc(sizeof(int));
if (memory != NULL)
{
  memory=10;
}
free(memory);

这也是一个错误。如果 malloc 失败,则将指针设置为 10 并释放该内存。 (和以前一样。)如果 malloc 成功,那么您将立即释放内存,这意味着分配它是没有意义的!现在,我想这只是为了表达要点而简化的示例代码,并且这在您的实际程序中不存在? :)

int* memory = NULL;
memory = malloc(sizeof(int));
if (memory != NULL)
{
  memory=10;
  free(memory);
}

This will crash. You're setting the pointer to memory location 10 and then asking the system to release the memory. It's extremely unlikely that you previously allocated some memory that happened to start at 10 (0xA in hexadecimal), even in the crazy world of virtual address spaces. Furthermore, IF malloc failed, no memory has been allocated, so you do not need to free it.

int* memory = NULL;
memory = malloc(sizeof(int));
if (memory != NULL)
{
  memory=10;
}
free(memory);

This is also a bug. If malloc fails, then you are setting the pointer to 10 and freeing that memory. (as before.) If malloc succeeds, then you're immediately freeing the memory, which means it was pointless to allocate it! Now, I imagine this is just example code simplified to get the point across, and that this isn't present in your real program? :)

不语却知心 2024-08-24 13:19:52

是的。重要的是太自由了一次。 (内存使用完毕后)

Yes. The important thing is too free it once. (After you are finished using the memory)

依 靠 2024-08-24 13:19:52

我喜欢遵循的一般经验法则是在分配内存的同一范围内释放内存。

A general rule of thumb that I like to follow is to free memory in the same scope that it was allocated in.

为人所爱 2024-08-24 13:19:52

首先,你只能释放它一次。因此,如果您想在循环内释放它(正如您的问题所建议的那样),第二个解决方案是唯一正确的方法。

如果您想在 if 语句中释放它,这两种解决方案在技术上都是正确的。然而,始终释放您使用的内存是一个好习惯,这样在任何 if 语句之外的末尾总是释放内存会更容易。

所以我的建议是:无论你想做什么:总是在任何循环/if语句之外释放内存(当然假设你不再需要它)。我通常在最后使用它的函数末尾释放它,但这实际上取决于函数本身,它的长度,......

尝试找到你的做事方式并坚持下去:它会为你节省很多无用的东西大脑时间(这真的很宝贵)

First of all you can only free it once. So if you want to free it inside a loop (as your question suggest), the second solution is the only correct way of doing.

If you want to free it inside an if statement, both solution are technically correct. However it is a good practice to always free the memory you use and its then easier to always free the memory at the end outside any if statement.

So my advice is : whatever you are trying to do : always free the memory outside any loop/if statement (assuming you don't need it anymore of course). I usually free it at the end of the function where it's last used but it really depends on the function itself, its length, ....

Try finding your way of doing things and stick to it : it will save you a lot of useless brain time (which is really precious)

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