程序终止后动态分配的内存

2021-12-24 00:00:00 c memory-management malloc memory-leaks c++

当一个包含动态分配内存(使用 malloc/new)而没有 free/delete 调用的 C/C++ 程序终止时,动态分配的内存会发生什么?操作系统是收回内存还是其他程序无法访问该内存?

When a C/C++ program containing the dynamically allocated memory(using malloc/new) without free/delete calls is terminated, what happens to that dynamically allocated memory? Does the operating system takes back the memory or does that memory becomes unaccessible to other programs?

推荐答案

我不认为语言标准有任何保证,但是支持稀疏虚拟内存和内存保护的现代操作系统(如 MacOS X、Linux、所有最新版本的 Windows 和所有当前制造的手机)在行为不良的进程(当它们终止时)后自动清理并为您释放内存.内存保持不可用,但是只要程序正在运行.

I don't think that there are any guarantees in the language standard, but modern operating systems which support sparse virtual memory and memory protection (such as MacOS X, Linux, all recent version of Windows, and all currently manufactured phone handsets) automatically clean up after badly-behaved processes (when they terminate) and free the memory for you. The memory remains unavailable, however as long as the program is running.

如果您在微控制器、MacOS 9 或更早版本、DOS 或 Windows 3.x 上进行编程,那么您可能需要担心内存泄漏会导致整个操作系统永久无法使用内存.

If you're programming on microcontrollers, on MacOS 9 or earler, DOS, or Windows 3.x, then you might need to be concerned about memory leaks making memory permenantly unavailable to the whole operating system.

相关文章