title: Vector of objects in stack memory
body: <p>I want to make a vector of objects, but I don't want to allocate them on the heap. I want to allocate them onto stack memory. But I'm running into issues.</p>
  
  <pre><code>struct Object { int x; Object(int x) { this-&gt;x = x; } };
  
  int main() {
      std::vector&lt;Object*&gt; v;
  
      for (int i = 0; i &lt; 5; i++) {
          Object o (i);
          v.push_back(&amp;o);
          std::cout &lt;&lt; &amp;o &lt;&lt; std::endl; // keeps printing the same mem address...
                                        // ...implying that these are all the same object
      }
  }
  </code></pre>
  
  <p>I have a suspicion that the object is deleting itself after going out of scope after each iteration of the for-loop.</p>
  
  <p>Now, I know that you can just do <code>v.push_back(new Object(i))</code> for each iteration and it would work. However, I do not want these objects allocated onto the heap where I need to manually manage the memory.</p>