A conclusion is drawn from the following code:
In the Windows operating system, the stack space is not freed, but if the existing stack space to meet the function of the operation, no longer apply for new stack space, but the function exits also does not release the stack space, if the function is not enough space to run, you need to apply for new space;
The space used for malloc dynamic requests is available for free after it is in heap space
#include <stdio.h>
#include <Windows.h>
int Hello1 ()
{
The size of the stack space occupied by the int arra[1000*60];//is approximately 0.2MB
int i=0;
for (i=0;i<1000;i++)
{
arra[i]=1024;
}
for (i=0;i<40;i++)//wait 4 seconds
{
Sleep (100);
printf ("arra[%d] is%d\n", arra[i]);
}
return 0;
}
int Hello2 ()
{
The size of the stack space occupied by the int arra[1000*150];//is approximately 0.5MB
int i=0;
for (i=0;i<1000;i++)
{
arra[i]=1024;
}
for (i=0;i<40;i++)//wait 4 seconds
{
Sleep (100);
printf ("arra[%d] is%d\n", arra[i]);
}
return 0;
}
int Hello3 ()
{
The size of the stack space occupied by the int arra[1000*100];//is approximately 0.4MB
int i=0;
for (i=0;i<1000;i++)
{
arra[i]=1024;
}
for (i=0;i<40;i++)//wait 4 seconds
{
Sleep (100);
printf ("arra[%d] is%d\n", arra[i]);
}
return 0;
}
int main ()
{
int i=56;
int ret;
char * TMP_BUF;
printf ("I is%d\n", i);
for (i=0;i<10;i++)
{
Sleep (1000);
printf ("Sleep%d seconds\n", i);
}
Hello1 () before execution, probably 0.2MB
Ret=hello1 ();
After execution, it's probably 0.4MB.
Hello2 ();
Hello2 () After execution, it's probably 0.7MB.
Hello3 ();
Hello3 () is still 0.7MB after execution, indicating that in a Windows system, the stack space will always increase without decreasing, but generally does not exceed the compiler default stack size (the programmer does not change the case)
printf ("Game over!");
for (i=0;i<5;i++)
{
printf ("Over wait%d seconds\n", i);
Sleep (1000);
}
tmp_buf= (char *) malloc (1024*1024);
for (i=0;i<5;i++)
{
printf ("Over wait%d seconds\n", i);
Sleep (1000);
}
Tmp_buf when the program runs, the memory size is 1.8MB before release
Free (TMP_BUF);
When the Tmp_buf is released, the program runs with a memory size of 0.7MB, which means free space can be freed directly.
for (i=0;i<5;i++)
{
printf ("Over wait%d seconds\n", i);
Sleep (1000);
}
return 0;
}
Research on the management of the program run-time stack by Windows operating system