title: Safe to return a vector populated with local variables?
body: <p>Is it safe to return a vector that's been filled with local variables?  </p>
  
  <p>For example, if I have...</p>
  
  <pre><code>#include &lt;vector&gt;
  
  struct Target
  {
  public:
      int Var1;
      // ... snip ...
      int Var20;
  };
  
  
  class Test
  {
  public:
      std::vector&lt;Target&gt; *Run(void)
      {
          std::vector&lt;Target&gt; *targets = new std::vector&lt;Target&gt;;
          for(int i=0; i&lt;5; i++) {
              Target t = Target();
              t.Var1 = i;
              // ... snip ...
              t.Var20 = i*2; // Or some other number.
              targets-&gt;push_back(t);
          }
          return targets;
      }
  
  
  };
  
  int main()
  {
      Test t = Test();
      std::vector&lt;Target&gt; *container = t.Run();
  
      // Do stuff with `container`
  }
  </code></pre>
  
  <p>In this example, I'm creating multiple <code>Target</code> instances in a for loop, pushing them to the vector, and returning a pointer to it.  Because the <code>Target</code> instances were allocated locally, to the stack, does that mean that the returned vector is unsafe because it's referring to objects on the stack (that may soon be overwritten, etc)?  If so, what's the recommended way to return a vector?  </p>
  
  <p>I'm writing this in C++, by the way.</p>