Analysis of stack growth direction and C code implementation

Source: Internet
Author: User

Analysis of stack growth direction and C code implementation

We should be familiar with the data structure such as stack. It is a type of post-import, first-out data structure. In a general computer system, there are two ways to store data in stacks: one is to increase upwards and the other is to increase downwards, as shown in 1.
Vc/yPC9wPg0KPHA + expires/expires + expires/Vu7XE1PazpLe9z/Kho8D708PV4rj2z + expires "brush: java; ">/*************************************** * ******************************** All Rights Reserved (C) 2015, Zhou Zhaoxiong. ** File name: FindStackDirection. c * File ID: none * Content Abstract: View stack growth direction * Other Description: none * Current version: V1.0 * Author: Zhou Zhaoxiong * completion date: 20151202 *************************************** * *******************************/# include // Redefine the Data Type typedef unsigned char UINT8; typedef signed int INT32; // function declaration void FindStackDirection (void ); /*************************************** * ******************************** function description: main function * input parameter: none * output parameter: none * return value: none * Other description: no * modified date version number modifier modified content * found * 20151202 V1.0 Zhou Zhaoxiong create *********************** **************************************** ******* */INT32 main () {FindStackDirection (); return 0 ;} /*************************************** * ******************************** function description: search for Stack growth direction * input parameter: none * output parameter: none * return value: none * Other Instructions: no * modified date version number modifier modified content * found * 20151202 V1.0 Zhou Zhaoxiong create *********************** **************************************** * ******/void FindStackDirection (void) {UINT8 iStackA Ddr = 0; // used to obtain the stack address static UINT8 * pStackAddr = NULL; // used to store the first iStackAddr address if (pStackAddr = NULL) // enter {pStackAddr = & iStackAddr for the first time; // Save the iStackAddr address FindStackDirection (); // recursion} else // enter {if (& iStackAddr> pStackAddr for the second time) // if the address of the second iStackDirection is greater than that of the first iStackDirection, the Stack growth direction is up {printf (Stack grows up !);} Else if (& iStackAddr <pStackAddr) // if the address of the second iStackDirection is smaller than that of the first iStackDirection, the Stack growth direction is downward {printf (Stack grows down !);} Else {printf (Bad stack !); }}}

We can see that there is a recursive call in the findstackction ction function, that is, when you first enter the function, assign the address value of the iStackAddr variable (local variable) to pStackAddr, when you enter the function for the second time, use the address value of the new iStackAddr variable to compare with the address value of the iStackAddr variable when you first enter the function. If the former is greater than the latter, the stack growth direction is upward. Otherwise, the stack growth direction is downward.

Upload the above Code to a Linux machine and use the "gcc-g-o FindStackDirection. c" command to compile the program and run the "FindStackDirection" command. The result is as follows:

Stack grows down!

That is, the stack growth direction in the system I use is downward. You can also run the above Code in your system to see what the result is.

In most people (including me), there is only one direction for Stack growth, that is, up (a in 1 )), however, the program runs out of the opposite result as we expected. From this point, we can also see that the design of computer systems is exquisite and complex, and there are many things worth further research.

 

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.