Research on the management of the program run-time stack by Windows operating system

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.