请问程序退出时堆得到释放? [英] Does the heap get freed when the program exits?

查看:81
本文介绍了请问程序退出时堆得到释放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我malloc的一些内存入一些指点,但在程序退出之前不释放他们。这是否内存获取退出时自动释放或将内存泄漏继续存在,直到我重新启动计算机?

Suppose I malloc some memory into some pointers but don't free them before the program exits. Does this memory get freed automatically on exit or will the memory leak continue to be there till I restart the computer?

推荐答案

答案是,最常见的。

释放堆是操作系统的责任心。虽然大多数的操作系统(尤其是主流OS)释放在退出堆,它不一定是说,嵌入式系统的操作系统。

Freeing the heap is responsiblity of the OS. While most OS (especially mainstream OS) frees the heap upon exit, it is not necessarily true of say embedded system OS.

当你调用内存要在堆中分配,系统调用是操作系统的内核空间提供这种记忆的。该存储器被映射到您的工艺结构,这是由OS维持。当你的程序退出时,操作系统经过一个清理路径,关闭所有的文件描述符,并标记这个内存免费分配给其他进程(除其他事项外)。

When you call for memory to be allocated on the heap, a system call is made to the kernel space of the OS to provide this memory. This memory is mapped to your process structure, which is maintained by the OS. When your program exits, the OS goes through a clean up routing, closing all file descriptors, and marks this memory free for allocation to other processes (among other things).

一些这些答案都在说,这是编译器相关的不正确。编译器不会说:'嘿释放的程序退出这一切的记忆。这是没有意义的,会发生什么,如果OS意外终止程序呢?不,编译器负责生成的系统调用的内存时分配/释放被明确要求用于堆。

Some of these answers are incorrect in saying that it is compiler dependant. The compiler does not say 'hey free all this memory on program exit'. That wouldn't make sense, what happens if the OS unexpectedly terminates the program then? No, the compiler is responsible for generating system calls whenever memory allocation/deallocation is explicitly requested for the heap.

这篇关于请问程序退出时堆得到释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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