title: Why does an object returned by value have the same address as the object inside the method?
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;array&gt;
  #include &lt;vector&gt;
  
  std::vector&lt;int&gt; foo() {
   int i;
   std::vector&lt;int&gt; a(100);
   printf("%p, %p, %p\n", &amp;i, &amp;a, &amp;(a[0]));
   return a;
  }
  
  int main() {
   int i;
   std::vector&lt;int&gt; b = foo();
   printf("%p, %p, %p\n", &amp;i, &amp;b, &amp;(b[0]));
  }
  </code></pre>
  
  <p>Why do <code>a</code> and <code>b</code> have the same address for the above? Is this some kind of "cross-stack-frame" optimization? The result is the same even when I use the <code>-O0</code> option.</p>
  
  <p>The output:</p>
  
  <pre><code>$ vim main.cpp 
  $ cc -std=c++11 -lc++ main.cpp
  $ ./a.out
  0x7ffee28d28ac, 0x7ffee28d28f0, 0x7ff401402c00
  0x7ffee28d290c, 0x7ffee28d28f0, 0x7ff401402c00
  $ 
  </code></pre>