为什么我需要手动释放动态内存?

时间:2017-08-28 18:54:22

标签: c dynamic-memory-allocation

在C中,我们了解到在程序结束时,我们动态分配内存,我们需要释放它,否则会出现内存泄漏。

#include <stdio.h>
int a = 17;
int main(void)
{
  int b = 18; //automatic stack memory
  int * c;
  c = malloc( sizeof( int ) ); //dynamic heap memory
  *c = 19;
  printf("a = %d at address %x\n", a, &a);
  printf("b = %d at address %x\n", b, &b);
  printf("c = %d at address %x\n", *c, c);
  free(c);  
  system("PAUSE");  
  return 0;
}

我的问题是,为什么我们需要手动?当程序结束时(如上例所示),内存是否会自行释放?

0 个答案:

没有答案