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

前端 未结 4 851
遇见更好的自我
遇见更好的自我 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:30

    the memory is actually not "free()"d at all.

    memory acquired by the operating system is page size (4kbytes of memory usually). whenever a process runs out of memory it acquires additional pages, these are the space malloc() actually uses. when a process terminates all pages are returned to the operating system making calling free actually unnecessary. if your programme is a server or similar every piece of memory that is never freed will only be returned when the programme is actually killed - making it every more memory hungry.

提交回复
热议问题