当你的malloc后不要随意到底发生了什么? [英] What REALLY happens when you don't free after malloc?

查看:114
本文介绍了当你的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:

首先,如果我有code这件事情是这样的:

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;
}

什么是真正的结果吗?我的想法是,进程死亡,然后堆空间反正走了这么有缺失调用无伤害免费(不过,我确实认识反正有它的重要性对于倒闭,可维护性和良好做法)。我说的对这个想法?

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?

二,假设我有一个行为有点像贝壳的程序。用户可以像声明 AAA = 123 变量和那些存储在以备后用一些动态的数据结构。显然,这似乎很明显,你会使用一些解决方案,调用一些*页头功能(HashMap中,链表,类似的东西)。对于这种方案,它没有任何意义永远免费通话的malloc后,因为这些变量必须在任何时候都present程序的执行过程中,有没有好的方法(我可以看到)与静态分配的空间来实现这一点。它是坏的设计有这种的分配,但只有释放的过程结束的一部分一堆记忆?如果是这样,有什么替代方案?

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程序所在的静态存储和运行​​时内存是pretty类似的事情,所以不会释放可能会导致程序占用更多的存储空间。 (我只是猜测这里。)

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天全站免登陆