当一个程序终止会发生什么用malloc分配的内存未free'ed? [英] When a program terminates what happens to the memory allocated using malloc that is not free'ed?

查看:122
本文介绍了当一个程序终止会发生什么用malloc分配的内存未free'ed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有下面的程序

#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;
}

以上程序调用的malloc 来分配一些内存,并且不调用免费来取消分配它。并且程序终止而不去分配内存。

The above program calls malloc to allocate some memory and does not call free to de-allocate it. And the program terminates without de-allocating the memory.

Valgrind的明确检测内存泄漏。

Valgrind clearly detects a memory leak.

<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)

问:

在程序终止,会发生什么情况已分配但不内存免费'D?

When the program terminates, what happens to the memory that was allocated but not free'd?

更新:
考虑到这code被不同的操作系统上执行 - 比如Windows,Linux和solarix,MacOS上,等有没有在这code的行为有什么区别其终止期间

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?

推荐答案

其他答案告诉你们这两个重要的事情:

The other answers tell you the two important things:


  1. 是的,内存由OS回收,所以你不要的技术上的需要免费()了。

  2. 这是很好的做法释放你啦malloced一切。

不过,可以说是非常重要的为什么的这是很好的做法您已经malloced免费()的一切。在我看来:

However, it's important to say why it's good practice to free() everything you've malloced. In my view:


  1. 习惯:如果您在释放只要您malloced的习惯得到的,你不会不小心,当内存段不在身边的程序的生命周期忘记

  2. 可维护性:如果有人走来重构你的程序,这样的内存段已不再是围绕该计划的生命周期,在清理code的presence原来将意味着它很可能是重构版本还包括清除code。对我来说,这是最重要的原因。

  3. 调试:如果我们希望所有的内存是否正确清理,然后点滴记忆那是的实际的泄漏是很容易

  1. Habit: If you get in the habit of freeing whenever you've malloced, you won't accidentally forget when a memory segment isn't around for the lifetime of the program.
  2. Maintainability: If someone comes along to refactor your program so that a segment of memory is no longer around for the lifetime of the program, the presence of cleanup code in the original will mean that it's very likely that the refactored version also includes the cleanup code. For me, this is the most important reason.
  3. Debugging: If we expect that all memory is cleaned up correctly, then spotting memory that's actually leaking is much easier.

这篇关于当一个程序终止会发生什么用malloc分配的内存未free'ed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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