How to tell if the stack is growing from the top down or bottom up

Source: Internet
Author: User
Tags bool
how to tell if the stack is growing from the top down or bottom up

Just now I came up with a method, the code is as follows, please advise:

void Stacktest () {int i = 0;     int j = 0;     int *PI = &i;       int *PJ = &j;     if (Pi < PJ) cout<< "Bottom Up" <<endl;  else cout<< "Top Down" <<endl; }

The compiler does not really guarantee that I will be in front of J or behind ...
If you really want to judge it, it's better to use a function call:

BOOL Comparestack (int *p) {int newint;         if (&newint < P) return true;        Address decrement else return false;     Address Increment} bool Getstackorder () {int oldint; Return Comparestack (&oldint); }

Compile options to turn off the inline pass parameter is a pointer to the local variable that was called at the previous level. The local variables of this level call must be stacked on top of the stack. That is to say, the address of the local variable of the two-tier call is compared


If it is limited to the x86 processor, do not judge, must be to the low address growth, not under the control of the application. Under X86 platform Linux, the address of memory allocated with functions such as malloc is growing upward. and the stack segment (note that stacks are stack, heap is heap, not the same thing) the address is growing downward.
To be simple to judge, the assembly code push, check the ESP register value is increased, or decreased.

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.