当您在 malloc 之后不释放时,真正会发生什么? [英] What REALLY happens when you don't free after malloc?

查看:57
本文介绍了当您在 malloc 之后不释放时,真正会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这件事一直困扰着我多年.

This has been something that has bothered me for ages now.

我们在学校都被教导(至少,我是)你必须释放分配的每个指针.不过,我对不释放内存的实际成本有点好奇.在一些明显的情况下,比如当 malloc 在循环内或线程执行的一部分中被调用时,释放非常重要,这样就不会出现内存泄漏.但请考虑以下两个示例:

We are all taught in school (at least, I was) that you MUST free every pointer that is allocated. I'm a bit curious, though, about the real cost of not freeing memory. In some obvious cases, like when malloc is called inside a loop or part of a thread execution, it's very important to free so there are no memory leaks. But consider the following two examples:

首先,如果我有这样的代码:

First, if I have code that's something like this:

int main()
{
    char *a = malloc(1024);
    /* Do some arbitrary stuff with 'a' (no alloc functions) */
    return 0;
}

真正的结果是什么?我的想法是进程死了,然后堆空间无论如何都消失了,所以错过对 free 的调用没有坏处(但是,我确实认识到无论如何拥有它对于关闭、可维护性和好的做法).我的想法正确吗?

What's the real result here? My thinking is that the process dies and then the heap space is gone anyway so there's no harm in missing the call to free (however, I do recognize the importance of having it anyway for closure, maintainability, and good practice). Am I right in this thinking?

第二,假设我有一个程序,它的行为有点像 shell.用户可以声明诸如aaa = 123 之类的变量,并将这些变量存储在一些动态数据结构中以备后用.显然,很明显您会使用一些解决方案来调用一些 *alloc 函数(散列图、链表等).对于这种程序,在调用 malloc 之后释放是没有意义的,因为这些变量在程序执行期间必须始终存在,并且没有好的方法(我可以看到)使用静态分配的空间实现这一点.拥有一堆已分配但仅作为进程结束的一部分释放的内存是否是糟糕的设计?如果是这样,还有什么选择?

Second, let's say I have a program that acts a bit like a shell. Users can declare variables like aaa = 123 and those are stored in some dynamic data structure for later use. Clearly, it seems obvious that you'd use some solution that will calls some *alloc function (hashmap, linked list, something like that). For this kind of program, it doesn't make sense to ever free after calling malloc because these variables must be present at all times during the program's execution and there's no good way (that I can see) to implement this with statically allocated space. Is it bad design to have a bunch of memory that's allocated but only freed as part of the process ending? If so, what's the alternative?

推荐答案

几乎每个现代操作系统都会在程序退出后恢复所有分配的内存空间.我能想到的唯一例外可能是 Palm OS,其中程序的静态存储和运行时内存几乎相同,因此不释放可能会导致程序占用更多存储空间.(我只是在这里推测.)

Just about every modern operating system will recover all the allocated memory space after a program exits. The only exception I can think of might be something like Palm OS where the program's static storage and runtime memory are pretty much the same thing, so not freeing might cause the program to take up more storage. (I'm only speculating here.)

所以一般来说,这没有什么坏处,除了运行时成本超出您的需要.当然,在您给出的示例中,您希望为可能会使用的变量保留内存,直到它被清除为止.

So generally, there's no harm in it, except the runtime cost of having more storage than you need. Certainly in the example you give, you want to keep the memory for a variable that might be used until it's cleared.

然而,在您不再需要内存时立即释放它被认为是一种很好的方式,并在程序退出时释放您仍然拥有的任何东西.这更像是一种了解您正在使用的内存并考虑您是否仍然需要它的练习.如果不跟踪,可能会出现内存泄漏.

However, it's considered good style to free memory as soon as you don't need it any more, and to free anything you still have around on program exit. It's more of an exercise in knowing what memory you're using, and thinking about whether you still need it. If you don't keep track, you might have memory leaks.

另一方面,在退出时关闭文件的类似警告有一个更具体的结果 - 如果你不这样做,你写给它们的数据可能不会被刷新,或者如果它们是一个临时文件,完成后它们可能不会被删除.此外,数据库句柄应该提交它们的事务,然后在完成它们后关闭.类似地,如果您使用的是 C++ 或 Objective C 等面向对象的语言,那么在完成后不释放对象将意味着永远不会调用析构函数,并且该类负责的任何资源可能不会被清理.

On the other hand, the similar admonition to close your files on exit has a much more concrete result - if you don't, the data you wrote to them might not get flushed, or if they're a temp file, they might not get deleted when you're done. Also, database handles should have their transactions committed and then closed when you're done with them. Similarly, if you're using an object oriented language like C++ or Objective C, not freeing an object when you're done with it will mean the destructor will never get called, and any resources the class is responsible might not get cleaned up.

这篇关于当您在 malloc 之后不释放时,真正会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆