How is dynamically allocated space freed when a program is interrupted using Ctrl-C?

前端 未结 4 859
遇见更好的自我
遇见更好的自我 2020-12-17 21:12

Given the following code:

#include 

int main()
{
    int *p;
    p = (int *)malloc(10 * sizeof(int));

    while(1);
    return 0;
}
<         


        
4条回答
  •  悲哀的现实
    2020-12-17 21:25

    When a process terminates, the operating system reclaims all the memory that the process was using.

    The reason why people make a big deal out of memory leaks even when the OS reclaims the memory your app was using when it terminates is that usually non-trivial applications will run for a long time slowly gobbling up all the memory on the system. It's less of a problem for very short-lifetime programs. (But you can never tell when a one-liner will become a huge program, so don't have any memory leaks even in small programs.)

提交回复
热议问题