问题描述
当程序退出时,malloc分配的内存会被释放吗?
我想它会从程序退出时开始,操作系统将释放此过程使用的所有内存(全局,堆栈,堆)。
是否正确?
Hi,
Will the memory allocated by malloc get released when program exits?
I guess it will since when the program exits, the OS will free all the
memory (global, stack, heap) used by this process.
Is it correct?
推荐答案
引用上下文会更好。
C标准和POSIX标准都没有明确定义
exit()[或POSIX _exit()]作为释放内存。 C标准
也没有将abort()定义为释放内存。
在实践中,我想不出任何多进程操作系统
当进程结束时,它不会释放malloc()的内存。
我不知道 - MVS可能吗? ;-)
我似乎记得(也许是错误的),当一个人在一个人运行时,不确定
内存将在进程结束时被释放
嵌入式环境 - 如果您的烤面包机控制程序不知何故
退出[例如,烤面包机着火?]那么有什么元流程可供
释放内存吗?
原始海报询问了malloc()的内存,而不是关于
所有内存。您使用的短语所有内存可以被解释为包括共享内存段,mmap()'d段
和其他非malloc( )''表格 - 形式超出C标准
但存在于众多系统中。特别是共享内存段
不一定在创建过程终止时释放。
-
注意,有骆驼!
It would be better to quote context.
Neither the C standard nor the POSIX standard explicitly define
exit() [or POSIX _exit()] as freeing memory. Nor does the C standard
define abort() as freeing memory.
In practice, I cannot think of any multi-process operating system
that does -not- free malloc()''d memory when the process ends.
I dunno -- MVS maybe? ;-)
I seem to recall (perhaps incorrectly) that it is not certain that
memory will be freed at process end when one is running in an
embedded environment -- if your toaster-control program somehow
exits [e.g., toaster on fire?] then what meta-process is there to
free the memory?
The original poster asked about malloc()''d memory, not about
"all the memory". The phrase you used, "all the memory", could
be construed to include shared memory segments, mmap()''d segments
and other non-malloc()''d forms -- forms that lie outside the C standard
but which exist on numerous systems. Shared memory segments in particular
are not necessarily released when the creating process terminates.
--
Look out, there are llamas!
这不一定是真的。某些版本的Windows会这样做,其他版本则没有。
Unix确实如此(至少是我用过的那些)。 VxWorks没有。
REH
That''s not necessarily true. Some versions of Windows do, others do not.
Unix does (At least the ones that I have used do). VxWorks does not.
REH
这篇关于当程序退出时,malloc分配的内存会被释放吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!