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] #