释放内存的重要性? [英] importance of freeing memory?

查看:186
本文介绍了释放内存的重要性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:

  <一href=\"http://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc\">What真正发生的时候,你不要&rsquo的;?吨的malloc后免费

在结束在C / C ++程序,你必须释放指针进行清理。如果你不释放内存,就像如果你有一个指针,指向一个int和结束程序时不会删除它会发生什么?是内存仍然使用,并且只能通过重启来释放,或程序结束时,它会自动释放?而在最后一种情况下,为什么免费呢,如果操作系统会为你?

When ending a program in C/C++, you have to clean up by freeing pointers. What happens if you doesn't free the memory, like if you have a pointer to an int and doesn't delete it when ending the program? Is the memory still used and can only be freed by restart, or is it automatically freed when the program ends? And in the last case, why free it, if the operating system does it for you?

推荐答案

当您的程序结束所有的内存会由操作系统被释放。

When your program ends all of the memory will be freed by the operating system.

您应该自己释放它的原因是,内存是你正在运行的程序中一种有限的资源。当然在极短的运行简单的程序,不能释放内存不会有noticable效果。但是长正在运行的程序,不能释放内存意味着你将没有补充它被消耗有限的资源。最终,它会耗尽你的程序会崩溃粗暴。这就是为什么你必须释放内存。

The reason you should free it yourself is that memory is a finite resource within your running program. Sure in very short running simple programs, failing to free memory won't have a noticable effect. However on long running programs, failing to free memory means you will be consuming a finite resource without replenishing it. Eventually it will run out and your program will rudely crash. This is why you must free memory.

这篇关于释放内存的重要性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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