[英]When a program terminates what happens to the memory allocated using malloc that is not free'ed?
Say I have the following program 说我有以下程序
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int * i;
if ((i = malloc(sizeof(int) * 100)) == NULL) {
printf("EROOR: unable to allocate memory \n");
return -1;
}
/* memory is allocated successfully */
/* memory is not free'ed but program terminates */
// free(i);
return 0;
}
The above program calls malloc
to allocate some memory and does not call free
to de-allocate it. 上面的程序调用
malloc
来分配一些内存,并且不会调用free
来解除分配。 And the program terminates without de-allocating the memory. 程序终止而不取消分配内存。
Valgrind clearly detects a memory leak. Valgrind清楚地发现内存泄漏。
<snap>
==14209== HEAP SUMMARY:
==14209== in use at exit: 400 bytes in 1 blocks
==14209== total heap usage: 1 allocs, 0 frees, 400 bytes allocated
==14209==
<sanp>
==14209== LEAK SUMMARY:
==14209== definitely lost: 400 bytes in 1 blocks
==14209== indirectly lost: 0 bytes in 0 blocks
==14209== possibly lost: 0 bytes in 0 blocks
==14209== still reachable: 0 bytes in 0 blocks
==14209== suppressed: 0 bytes in 0 blocks
==14209==
==14209== For counts of detected and suppressed errors, rerun with: -v
==14209== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Question: 题:
When the program terminates, what happens to the memory that was allocated but not free
'd? 当程序终止时,分配但不是
free
的内存会发生什么?
Update: Consider that this code is being executed on different operation system - say windows, linux, solarix, macos, etc. Is there any difference in the behavior of this code during its termination? 更新:考虑这个代码是在不同的操作系统上执行的 - 比如windows,linux,solarix,macos等。这个代码在终止期间的行为有什么不同吗?
The other answers tell you the two important things: 其他答案告诉你两个重要的事情:
free()
it. free()
它。 However, it's important to say why it's good practice to free()
everything you've malloced. 但是,重要的是要说明为什么
free()
所有你已经编写过的东西都是好的做法。 In my view: 在我看来:
OS will reclaim the memory not free'd up. 操作系统将回收未释放的内存。
But is a good practice to free all memory allocated by malloc
但释放
malloc
分配的所有内存是一种很好的做法
The memory is reclaimed by the Operating system once your program exits. 程序退出后,操作系统将回收内存。
The OS doesn't understand that your program leaked memory, it simply allocates memory to the program for running and once the program exits it reclaims that memory. 操作系统不了解您的程序泄漏内存,它只是将内存分配给程序进行运行,一旦程序退出就回收内存。
However, other resources like file descriptors may/may not be recalimed by the OS causing a resource leak. 但是,操作系统可能会/可能不会重新调整文件描述符等其他资源,从而导致资源泄漏。
So it is a good practice that a program should cleanup all the resource it utilized before exiting. 因此,一个好的做法是程序应该在退出之前清理它所使用的所有资源。
When a process allocates memory dynamically, it borrows block(s) of memory from OS. 当进程动态分配内存时,它会从OS借用内存块。 When a process doesn't need allocated memory it free(s).
当一个进程不需要分配内存时,它就会释放。 Then OS adds those blocks to its free list.
然后OS将这些块添加到其空闲列表中。 The same happens when a process terminates.
当进程终止时也会发生同样的情况。 All the blocks used by the process is reclaimed by the OS.
操作系统回收该过程使用的所有块。
更重要的是,FREE确保您分配的内存/缓冲区的健全性,从而存在一个良好的检查点来抑制/追赶堆损坏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.