title: why addresses of elements in the stack are reversed in ubuntu64?
body: <p>I write a simple program to print out the addresses of the elements in the stack</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;memory.h&gt;
  void f(int i,int j,int k)
  {
      int  *pi = (int*)malloc(sizeof(int));
      int a =20;
      printf("%p,%p,%p,%p,%p\n",&amp;i,&amp;j,&amp;k,&amp;a,pi);
  }
  
  int main()
  {
      f(1,2,3);
      return 0;
  }
  </code></pre>
  
  <p>output:(in ubuntu64, <strong>unexpected</strong>)</p>
  
  <pre><code>0x7fff4e3ca5dc,0x7fff4e3ca5d8,0x7fff4e3ca5d4,0x7fff4e3ca5e4,0x2052010
  </code></pre>
  
  <p>output:(in ubuntu32 , <strong>as expected</strong>)</p>
  
  <pre><code>0xbf9525f0,0xbf9525f4,0xbf9525f8,0xbf9525d8,0x931f008
  </code></pre>
  
  <p>environment for ubuntu64:</p>
  
  <pre><code>$uname -a
  Linux  3.8.0-26-generic #38-Ubuntu SMP Mon Jun 17 21:43:33 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
  $gcc -v
  Target: x86_64-linux-gnu
  gcc version 4.8.1 (Ubuntu 4.8.1-2ubuntu1~13.04)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/zmzzR.png" alt="enter image description here"></p>
  
  <p>According to the diagram above, that the earlier the element has been pushed to the stack, the higher address it will locate,
  and if using calling convention cdecl , the rightest parameter will be push to the stack first.
  The local variable should be pushed to the stack after pushed the parameters</p>
  
  <p>But the output is reversed in ubuntu64 as expected:</p>
  
  <pre><code>the address of k is :0x7fff4e3ca5d4   //&lt;---should have been pushed to the stack first
  the address of j is :0x7fff4e3ca5d8   
  the address of i is :0x7fff4e3ca5dc   
  the address of a is :0x7fff4e3ca5e4   //&lt;---should have been pushed to the stack after i,j,k
  </code></pre>
  
  <p>Any ideas about it?</p>