我能避免释放已分配的内存用C拥有现代化的操作系统? [英] Can I avoid releasing allocated memory in C with modern OSes?

查看:123
本文介绍了我能避免释放已分配的内存用C拥有现代化的操作系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual C ++ 2010的一个C项目。我有以下code的main():

I am using Visual C++ 2010 for a C project. I have the following code in main():

ARRAY2D a;

arr2_init(&a, 5, 5);  /* 5x5 dynamic, multi-demensional array (of type int) */
arr2_release(&a); 

我不知道如果我需要的最后一行。我可以省略了在现代操作系统的程序的结束arr2_release()?我使用Windows 7。

I'm not sure if I need the last line. Can I omit out arr2_release() at the end of the program in modern OS's? I'm using Windows 7.

推荐答案

是的,你能避免手动释放任何资源了运行时或操作系统将在后面进行清理。结果
不过,不这样做吧。

Yes, you can avoid releasing any resource manually which the runtime or the OS will clean up after you.
Still, do not do so please.

这是更快的关闭一个有效的优化(有时甚至为换取内存消耗更快的执行),但是你一定要挑剔你离开它周围的资源:

It is a valid optimisation for faster shutdown (and sometimes even for faster execution in exchange for memory consumption), though you must be picky about which resources you leave around:


  • 内存和文件描述符有效地被处理的操作系统(古代平台不这样做大多死于废弃。不过,也有一些微小的系统无法释放此)。

  • 文件缓冲区有效地运行清理。

  • Windows图形用户界面资源的不可以有效地清理了这种方式,它需要更长的时间。

  • Memory and file descriptors are efficiently handled by the OS (ancient platforms not doing so have mostly succumbed to disuse. Still, there are a few tiny systems not able to free this).
  • FILE buffers are efficiently cleaned up by the runtime.
  • Windows GUI resources are not efficiently cleaned up this way, it needs longer.

总之,做好清理和制定正确的思维定式,这让寻找泄漏更容易,不如转移到更大和更长运行的任务。结果
premature优化是一切罪恶的根源。 (专家唯一的选择,以测量和慎重考虑后,优化还不适用)

Anyway, do the cleanup and develop the right mind-set, it makes searching for leaks much easier and is better transferable to bigger and longer-running tasks.
Premature optimisation is the root of all evil. (The expert only option to optimise after measurement and careful consideration does not apply yet)

这篇关于我能避免释放已分配的内存用C拥有现代化的操作系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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