本文介绍了当您退出一个C程序,是的malloc-ED自动记忆中解脱出来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有下面的C code:

Let's say I have the following C code:

int main () {
  int *p = malloc(10 * sizeof *p);
  *p = 42;
  return 0;  //Exiting without freeing the allocated memory
}

当我编译并执行C程序,将我分配的内存中分配还是在内存中分配一定的空间后IE浏览器(即基本上占用空间)后,我退出应用程序和进程终止?

When I compile and execute that C program, ie after allocating some space in memory, will that memory I allocated be still allocated (ie basically taking up space) after I exit the application and the process terminates?

推荐答案

这取决于操作系统。它结束时,大多数现代(和所有主要的)操作系统将释放不是由程序释放的内存。

It depends on the operating system. The majority of modern (and all major) operating systems will free memory not freed by the program when it ends.

就凭这是不好的做法,这是最好明确释放它。这个问题不只是你的code很糟糕。你可以决定要你的小程序集成到一个更大的,长期运行的。随后一段时间后你必须花时间跟踪内存泄漏。结果
依托操作系统的一项功能也使code的可移植性。

Relying on this is bad practice and it is better to free it explicitly. The issue isn't just that your code looks bad. You may decide you want to integrate your small program into a larger, long running one. Then a while later you have to spend hours tracking down memory leaks.
Relying on a feature of an operating system also makes the code less portable.

这篇关于当您退出一个C程序,是的malloc-ED自动记忆中解脱出来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 05:25
查看更多