title: internal address space reuse with vectors created inside a loop
body: <p>The contrived example below shows the duplicate addresses seen inside of a loop. The same phenomenon manifests when I build both with and without optimization on MinGW GCC 4.5.0. The problem I'm seeing is that when I create a vector inside a loop, the internal array sometimes appears to occupy the same memory addresses as that of the vector created on the previous iteration. My question is, what causes this?</p>
  
  <p>The question is relevant to me because I had some code that (essentially) passed a reference to a vector created inside a loop to a new thread. The threads were trampling each other and causing the program to crash. When I changed the code to create the vectors outside of the loop, the program ran fine and produced the same results as the single threaded version of my program.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using std::cout;
  using std::endl;
  #include &lt;vector&gt;
  using std::vector;
  #include &lt;cstdlib&gt;
  //using NULL;
  #include &lt;algorithm&gt;
  using std::for_each;
  #include &lt;tr1/functional&gt;
  using std::tr1::bind;
  using std::tr1::placeholders::_1;
  
  template&lt;typename T&gt;
  void write_address(T const &amp; arg){
      cout&lt;&lt;" "&lt;&lt;&amp;arg;
  }
  
  template &lt;typename T&gt;
  void write_container_addresses(vector&lt;T&gt; const &amp; container){
      for_each(container.begin(),container.end(),
              write_address&lt;T&gt;);
      cout&lt;&lt;endl;
  }
  
  template &lt;typename T&gt;
  void write_container_container_addresses(
          vector&lt;vector&lt;T&gt; &gt; const &amp; container){
      for_each(container.begin(),container.end(),
              write_container_addresses&lt;T&gt;);
  }
  
  int main() {
      vector&lt;vector&lt;int&gt; &gt; stacked_vector(5);
      for_each(stacked_vector.begin(),stacked_vector.end(),
              bind(&amp;vector&lt;int&gt;::reserve,_1,9));
      for_each(stacked_vector.begin(),stacked_vector.end(),
              bind(&amp;vector&lt;int&gt;::resize,_1,5,0));
  
      cout&lt;&lt;"stacked vector addresses"&lt;&lt;endl;
      for (size_t i = 0;i&lt;stacked_vector.size();i++){
          write_container_addresses&lt;int&gt;(stacked_vector[i]);
      }
      cout&lt;&lt;endl;
  
      cout&lt;&lt;"another print of stacked vector addresses"&lt;&lt;endl;
      write_container_container_addresses(stacked_vector);
      cout&lt;&lt;endl;
  
      vector&lt;vector&lt;int&gt; &gt; other_stacked_vector;
  
      cout&lt;&lt;"other vector addresses"&lt;&lt;endl;
      for (size_t i = 0;i&lt;stacked_vector.size();i++){
          other_stacked_vector.push_back(vector&lt;int&gt;());
          other_stacked_vector.back().reserve(9);
          other_stacked_vector.back().resize(5,0);
          write_container_addresses&lt;int&gt;(other_stacked_vector[i]);
      }
      cout&lt;&lt;endl;
  
      cout&lt;&lt;"another write of other vector addresses"&lt;&lt;endl;
      write_container_container_addresses&lt;int&gt;(other_stacked_vector);
  
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>C:\workspace\test_of_pointer_vector_addresses\Debug&gt;test_of_pointer_vector_addre
  sses.exe
  stacked vector addresses
   0x3e3e08 0x3e3e0c 0x3e3e10 0x3e3e14 0x3e3e18
   0x3e3e38 0x3e3e3c 0x3e3e40 0x3e3e44 0x3e3e48
   0x3e3e68 0x3e3e6c 0x3e3e70 0x3e3e74 0x3e3e78
   0x3e3e98 0x3e3e9c 0x3e3ea0 0x3e3ea4 0x3e3ea8
   0x3e3ec8 0x3e3ecc 0x3e3ed0 0x3e3ed4 0x3e3ed8
  
  another print of stacked vector addresses
   0x3e3e08 0x3e3e0c 0x3e3e10 0x3e3e14 0x3e3e18
   0x3e3e38 0x3e3e3c 0x3e3e40 0x3e3e44 0x3e3e48
   0x3e3e68 0x3e3e6c 0x3e3e70 0x3e3e74 0x3e3e78
   0x3e3e98 0x3e3e9c 0x3e3ea0 0x3e3ea4 0x3e3ea8
   0x3e3ec8 0x3e3ecc 0x3e3ed0 0x3e3ed4 0x3e3ed8
  
  other vector addresses
   0x3e3f10 0x3e3f14 0x3e3f18 0x3e3f1c 0x3e3f20
   0x3e3f10 0x3e3f14 0x3e3f18 0x3e3f1c 0x3e3f20
   0x3e3f10 0x3e3f14 0x3e3f18 0x3e3f1c 0x3e3f20
   0x3e2430 0x3e2434 0x3e2438 0x3e243c 0x3e2440
   0x3e2430 0x3e2434 0x3e2438 0x3e243c 0x3e2440
  
  another write of other vector addresses
   0x3e3f40 0x3e3f44 0x3e3f48 0x3e3f4c 0x3e3f50
   0x3e3f60 0x3e3f64 0x3e3f68 0x3e3f6c 0x3e3f70
   0x3e24c8 0x3e24cc 0x3e24d0 0x3e24d4 0x3e24d8
   0x3e24e8 0x3e24ec 0x3e24f0 0x3e24f4 0x3e24f8
   0x3e2430 0x3e2434 0x3e2438 0x3e243c 0x3e2440
  </code></pre>