title: Pushing pointers into c++ vectors, and cleanup
body: <p>I am converting some code between different systems, and I have a question regarding c++ vectors.</p>
  
  <p>If I do something like this:</p>
  
  <p>In header file:</p>
  
  <pre><code>struct Vertex
  {
      float x;
      float y;
      float z;
  }
  
  struct submesh
  {
      Vertex *meshdata;
  }
  
  std::vector&lt;submesh&gt; meshes;
  </code></pre>
  
  <p>In a routine in the c++ file:</p>
  
  <pre><code>{
      Vertex *data = new Vertex[1024];
  
      submesh g;
      g.meshdata = data;
      meshes.push_back(g);
  
      delete [] data;
  }
  </code></pre>
  
  <p>Will I be in trouble? My assumption is that the vector would hold a pointer to data that is no longer valid once I called <strong>delete</strong> on it. Do I need to write a copy constructor for <strong>Vertex</strong> so that the data is copied first?</p>
  
  <p><strong>Additional:</strong></p>
  
  <p>The question was more to do with how do I put a pointer to allocated memory into a std::vector&lt;> and still cleanup the locally allocated data. Essentially, how do I copy the data into the vector so I can still clean up my copy.</p>
  
  <p>The original code was in DirectX. I am porting it to the iPhone. The original code allocated a submesh locally in a routine using:</p>
  
  <pre><code>{
      ID3DXMesh* subMesh = 0;
      D3DXCreateMesh(SubGrid::NUM_TRIS, SubGrid::NUM_VERTS, D3DXMESH_MANAGED, elems, gd3dDevice, &amp;subMesh));
  
      //
      // ... do some magical things to submesh
      //
  
      SubGrid g;
      g.mesh = subMesh;
      g.box  = bndBox;
      mSubGrids.push_back(g);
  }
  </code></pre>
  
  <p>I am trying to duplicate how ID3DXMesh is able to be added to a vector, then lose it's scope in the routine.</p>
  
  <p>As I don't have access to D3DXCreateMesh(), I figured I would simply allocate the vertices I needed, throw them into a vector, and clean up.</p>
  
  <p>Sorry, I wanted to keep the nitty gritty details out of it, as the question is simply how do I allocate a chunk of data, put a pointer into a std::vector&lt;>, then clean up the locally allocated memory. :)</p>
  
  <p>I assumed a copy constructor had to be written somewhere. Just wasn't sure where or how.</p>
  
  <p>A subgrid looks like this:</p>
  
  <pre><code>struct SubGrid
  {
      ID3DXMesh* mesh;
      AABB box;
  
      // For sorting.
      bool operator&lt;(const SubGrid&amp; rhs)const;
  
      const static int NUM_ROWS  = 33;
      const static int NUM_COLS  = 33;
      const static int NUM_TRIS  = (NUM_ROWS-1)*(NUM_COLS-1)*2;
      const static int NUM_VERTS = NUM_ROWS*NUM_COLS;
  };
  </code></pre>
  
  <p>And the vector they get added to looks like:</p>
  
  <pre><code>std::vector&lt;SubGrid&gt; mSubGrids;
  </code></pre>