title: Global vector emptying itself between calls?
body: <p>I have a vector in a header, like so:</p>
  
  <pre><code>extern std::vector&lt;Foo&gt; g_vector;
  </code></pre>
  
  <p>In the associated cpp file I have this:</p>
  
  <pre><code>std::vector&lt;Foo&gt; g_vector;
  </code></pre>
  
  <p>I also have a class <code>Bar</code>, and in it's constructor it will add some stuff to <code>g_vector</code>, like so:</p>
  
  <pre><code>Bar::Bar(/* stuff */)
  {
      // do things
      std::cout &lt;&lt; g_vector.size() &lt;&lt; std::endl;
      g_vector.push_back(somefoo);
      std::cout &lt;&lt; g_vector.size() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>If I declare a <code>Bar</code> inside a function, like a sane person, it appears to work fine. However, if I want to declare a <code>Bar</code> outside of a function, weird things happen. For example, I have a <code>Bar</code> declared in MyFile1.cpp and in MyFile2.cpp, and because of my cout statements in Bar I can see the <code>Foo</code> get pushed into the vector, but when the next <code>Bar</code> runs its constructor the vector's size is 0 again. In other words, my output is </p>
  
  <pre><code>0
  1
  0
  1
  </code></pre>
  
  <p>What gives? Just to be extra double sure, I also tried printing out <code>&amp;g_vector</code> to make sure it was actually <code>push_back</code>ing into the right vector, and the addresses all match. For what it's worth, it doesn't matter what order these things go in to the vector. I'm not concerned with the initialization order or anything.</p>