正在释放分配的内存需要退出在C程序时 [英] Is freeing allocated memory needed when exiting a program in C

查看:128
本文介绍了正在释放分配的内存需要退出在C程序时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用的malloc我分配的内存在我的C程序,现在我要退出,我必须释放分配的内存,或者我可以假设,因为我的整个程序终止时,它会被OS释放?

If I allocated memory in my C program using malloc and now I want to exit, do I have to free the allocated memory, or can I assume that since my entire program terminates, it will be freed by the OS?

我的Linux环境下运行。

I run in Linux environment.

推荐答案

任何现代操作系统将清理一切的过程结束之后,但它通常并不是靠这一个很好的做法。

Any modern operating system will clean up everything after a process terminates, but it's generally not a good practice to rely on this.

这取决于你正在写的程序。如果它只是运行并迅速终止一个命令行工具,你可能不打扰清理。但要知道,这是这种心态,导致内存泄漏的守护程序和长时间运行的程序。

It depends on the program you are writing. If it's just a command line tool that runs and terminates quickly, you may not bother cleaning up. But be aware that it is this mindset that causes memory leaks in daemons and long-running programs.

这篇关于正在释放分配的内存需要退出在C程序时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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