Local variable, global variable? Find the maximum stack space on the program

Source: Internet
Author: User

Recently, I have seen some interesting information related to programs and memory. It can reflect the memory relationship between global data and local variables. The result I run on cygwin is that the stack space is only 2 MB. After it is used up, the program stops running. The source code is as follows:

Filename: depletestacksize. c
# Include

Static long stack_block_header;

Void depletestackspace () {// declare local variable for booking some stack spaces.

Int intvarinstackspace = 0; // output the memory offset

Printf ("% 3.1fm/N", (stack_block_header-(long) (& intvarinstackspace) * 1.0)/(1024*1024); // self recursive depletestackspace ();

}

 

Int main () {// The first local variable in application memory.

Int intvarinstackspace = 0; // get first local variable address as the stack block Header

Stack_block_header = (long) (& intvarinstackspace); // perform depletion

Depletestackspace (); // out the normal Exit message

Printf ("normal exit./N ");}

 

Compile the Win32 program with GCC in cygwin:

[Root @ sharkwang temp] # gcc-MnO-cygwin-O depletestacksize.exe depletestacksize. c

 

The execution result is excerpted as follows:
2.0 m
2.0 m
2.0 m <--- % 3.1f is rounded up after formatting, so 1.5xxm and above are changed to m
2.0 m
2.0 m
2.0 m
2.0 m
2.0 m <--- the program stops after reaching the maximum space, and no output of normal exit is displayed...
[Root @ sharkwang temp] #

 

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.