Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's nothing in the standard that mandates how things are organized on the stack at all. In fact, you could build a conforming compiler that didn't store array elements at contiguous elements on the stack at all, provided it had the smarts to still do array element arithmetic properly (so that it knew, for example, that a<a href="https://stackoverflow.com/questions/664744/what-is-the-direction-of-stack-growth-in-most-modern-systems/664779#664779">1</a> was 1K away from a[0] and could adjust for that).</p> <p>The reason you may be getting different results is because, while the stack may grow down to add "objects" to it, the array is a single "object" and it may have ascending array elements in the opposite order. But it's not safe to rely on that behaviour since direction can change and variables could be swapped around for a variety of reasons including, but not limited to:</p> <ul> <li>optimization.</li> <li>alignment.</li> <li>the whims of the person the stack management part of the compiler.</li> </ul> <p>See <a href="https://stackoverflow.com/questions/664744/what-is-the-direction-of-stack-growth-in-most-modern-systems/664779#664779">here</a> for my excellent treatise on stack direction :-)</p> <p>In answer to your specific questions:</p> <ol> <li>Does stack grow up or down?<br> It doesn't matter at all (in terms of the standard) but, since you asked, it can grow up <em>or</em> down in memory, depending on the implementation.</li> <li>What happen between a[2] and q memory addresses? Why there are a big memory difference there? (20 bytes)?<br> It doesn't matter at all (in terms of the standard). See above for possible reasons.</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload