title: Are variables for pass by value variables and local variables in a function not allocated continuously?
body: <p>I want to know the way variables are allocated on stack, so i make a small test program</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  using namespace std;
  
  class test
  {
  public:
      test()
      {
  
      }
      test(const test &amp;obj)
      {
          cout &lt;&lt; "a clone of test has been created" &lt;&lt; endl;
      }
  };
  
  void foo(test variable_01, test variable_02)
  {
      cout &lt;&lt; "v3 and v4 have not been created" &lt;&lt; endl;
      char variable_03;
      char variable_04;
      cout &lt;&lt; "v3 and v4 have been created" &lt;&lt; endl;
  
      cout &lt;&lt; "Adrress of variable 01: " &lt;&lt; static_cast&lt;void*&gt;(&amp;variable_01) &lt;&lt; endl;
      cout &lt;&lt; "Adrress of variable 02: " &lt;&lt; static_cast&lt;void*&gt;(&amp;variable_02) &lt;&lt; endl;
      cout &lt;&lt; "Adrress of variable 03: " &lt;&lt; static_cast&lt;void*&gt;(&amp;variable_03) &lt;&lt; endl;
      cout &lt;&lt; "Adrress of variable 04: " &lt;&lt; static_cast&lt;void*&gt;(&amp;variable_04) &lt;&lt; endl;
  }
  
  
  int main()
  {
      test temp;
      foo(temp,temp);
      return 0;
  }
  </code></pre>
  
  <p>And here is the result</p>
  
  <pre><code>a clone of test has been created
  a clone of test has been created
  v3 and v4 have not been created
  v3 and v4 have been created
  Adrress of variable 01: 0xbf9dd3ae
  Adrress of variable 02: 0xbf9dd3af
  Adrress of variable 03: 0xbf9dd37e
  Adrress of variable 04: 0xbf9dd37f
  </code></pre>
  
  <p>We have two groups of variable here</p>
  
  <ul>
  <li>Copies of pass by value variables : v_01 &amp; v_02 (G1)</li>
  <li>Local variables : v_03 &amp; v_04 (G2)</li>
  </ul>
  
  <p>Base on the result, we know G1 have been allocated before G2 (maybe v_01 -> v_02 -> v_03 -> v_04). However there are a some strange things : </p>
  
  <ul>
  <li>Every variable in each groups are allocated continuously, but G1 and G2 are seem to be a bit far from each other , I think they should all be 0xbf9dd3a_ or 0xbf9dd37_ or { 0xbf9dd3ae -> 0xbf9dd3b1} or { 0xbf9dd37e -> 0xbf9dd381}</li>
  <li>Address of G1 is always greater address of G2. I can explain this base on the way stack work and data segment of a program when loaded. Here is a shortcut of data segment of a loaded program (source : <a href="http://www.inf.udec.cl/~leo/teoX.pdf" rel="nofollow noreferrer">http://www.inf.udec.cl/~leo/teoX.pdf</a> ) . </li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/peTUg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/peTUg.png" alt="enter image description here"></a></p>
  
  <ul>
  <li>However if my explanation above is right, I can't explain why in each groups, address of variables are base on the time they have been created. (ex : v_03 before v_04, so v_04's address is greater than v_03's one)</li>
  </ul>
  
  <p>Why address of G1 is greater G2, despite being created before G2?</p>
  
  <p>Why variables in each groups are allocated continuously, but these groups are a bit far from each other?</p>
  
  <p>Why address of variables in groups have different order compare to address order between these groups?</p>