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

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

问题描述

假设我有以下程序

#include <stdio.h>
#include <stdlib.h>

int main(void) 
{
    int * i;

    if ((i = malloc(sizeof(int) * 100)) == NULL) {
        printf("EROOR: unable to allocate memory 
");
        return -1;
    }

    /* memory is allocated successfully */

    /* memory is not free'ed but program terminates */
    // free(i);

    return 0;
}

上面的程序调用malloc来分配一些内存,并没有调用free来释放它.并且程序在不取消分配内存的情况下终止.

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)

问题:

当程序终止时,已分配但未free'd的内存会发生什么变化?

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

更新:考虑到这段代码是在不同的操作系统上执行的——比如 windows、linux、solarix、macos 等.这段代码在终止期间的行为有什么不同吗?

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. 是的,内存由操作系统回收,因此您技术上不需要free()它.
  2. 无论如何,释放您分配的所有内容是一种很好的做法.

然而,重要的是要说明为什么free()你已经分配的所有东西是一个好习惯.在我看来:

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

  1. 习惯:如果您养成了每次分配内存时都释放的习惯,您就不会意外地忘记在程序的整个生命周期内内存段何时不存在.
  2. 可维护性:如果有人来重构您的程序,使得一段内存在程序的生命周期内不再存在,那么原始代码中存在的清理代码将意味着它非常重构版本可能还包含清理代码.对我来说,这是最重要的原因.
  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 分配的内存会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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