是否需要释放每个malloc调用 [英] Does every malloc call have to be freed

查看:102
本文介绍了是否需要释放每个malloc调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,因为malloc动态分配了mem,所以您需要释放该mem以便再次使用.

From what I understand because malloc dynamically assigns mem , you need to free that mem so that it can be used again.

  1. 如果返回使用malloc创建的char *(即应该如何释放它)会发生什么情况
  2. 如果将指针保持原样 并退出应用程序 释放.(我对此没有明确的答案,有人说是",有人说不").
  1. What happens if you return a char* that was created using malloc (i.e. how are you supposed to free that)
  2. If you leave the pointer as it is and exit the application will it be freed.(I cant find a definite answer on this , some say yes , some say no).

推荐答案

  1. 调用者必须释放它(或安排释放它).这意味着创建和返回资源的函数需要准确记录如何释放资源.

  1. The caller has to free it (or arrange for it to be freed). This means that functions that create and return resources need to document exactly how it should be freed.

大多数操作系统将在程序退出时释放内存,这是进程"定义的一部分. C标准不在乎会发生什么,它超出了程序的范围.并非所有的操作系​​统都具有完整的进程抽象,但是台式机风格的操作系统当然可以.

Most OSes will free the memory when the program exits, as part of the definition of a "process". The C standard doesn't care what happens, it's beyond the scope of the program. Not all OSes have a full process abstraction, but desktop-style OSes certainly do.

在此之前释放它的主要原因是:

The main reasons to free it before that are:

  • 如果您在进程退出前很长时间(通常是很长时间)内尽快释放内存,那么程序将使用更少的内存.
  • 如果您不释放它,而以后又想将您的程序更改为另一个程序中的一个例程(可能被多次调用),那么突然您需要的存储量将是以前的许多倍(内存泄漏). /li>
  • 有一些调试工具可以通过警告您程序退出时仍在分配的内存来帮助您识别内存泄漏.如果有很多故意泄漏的垃圾要经过,这些对实际上没有多大帮助.
  • 如果您不释放它,而遇到任何问题,那么比起一开始就难得多,以后再返回并查​​找所有需要释放的内存要困难得多.
  • 在很多情况下,您确实需要释放内存(以防止长时间运行的程序占用大量内存),因此您的默认策略必须是清理几乎所有内容.
  • If you free memory as soon as possible, often a long time before process exit, your program uses less memory total.
  • If you don't free it, and you later want to change your program into a routine within another program, that perhaps is called many times, then suddenly you require many times as much memory as before (memory leak).
  • There are debugging tools that will help you identify memory leaks, by warning you about memory that is still allocated when the program exits. These don't really help much if there's a lot of deliberately-leaked junk to wade through.
  • If you don't free it and you hit any problems, it's much harder to go back later and find all the memory that needs freeing, than it is to do it right in the first place.
  • There are so many cases where you do need to free the memory (to prevent huge memory use in long-running programs), that your default strategy must be to clean pretty much everything up anyway.

不自由的隐约理由是:

  • 更少的代码.
  • 如果要在程序退出前立即释放成千上万的块,那么让OS放弃整个过程可能会更快.
  • 按需创建并存储在全局变量中的东西可能很难安全清理,如果您不确切知道它的使用位置.想一想,随着时间的流逝会填充某种类型的缓存,该缓存可能具有MRU规则来限制其占用的内存量,因此这并不是无限的泄漏.好的,这是一件不好的事情(不受限制的全局变量),导致另一件不好的事情(未释放的内存),但是作为一个原因,您可能会在现有代码中看到未释放的块,因此值得了解,您不一定要去修复它他们.

释放的原因几乎总是超过反对的原因.

The reasons for freeing almost always outweigh the reasons against.

这篇关于是否需要释放每个malloc调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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