title: Vector.push_back Issue
body: <p>The following question concerns the use of a vector and memcpy. Vector functions being used are, .push_back, .data(), .size().</p>
  
  <p>Information about the msg.</p>
  
  <p>a)</p>
  
  <pre><code>#define BUFFERSIZE 8&lt;&lt;20
  char* msg = new char[(BUFFERSIZE / 4)];
  </code></pre>
  
  <p>Question: Why doesn't code block b) work?</p>
  
  <p>When I run the code below, using vector.push_back in a for loop, it causes the software I'm working with to stop working. I'm not sending the "msg" nor am I reading it, I'm just creating it.</p>
  
  <p>b)</p>
  
  <pre><code>mVertex vertex;
  vector&lt;mVertex&gt;mVertices;
  for (int i = 0; i &lt; 35; i++)
  {
  vertex.posX = 2.0;
  vertex.posY = 2.0;
  vertex.posZ = 2.0;
  vertex.norX = 2.0;
  vertex.norY = 2.0;
  vertex.norZ = 2.0;
  vertex.U = 0.5;
  vertex.V = 0.5;
  mVertices.push_back(vertex);
  }
  memcpy(msg,                                     // destination
      mVertices.data(),                           //  content  
      (mVertices.size() * sizeof(mVertex)));      // size
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/VBPUa.png" rel="nofollow noreferrer">Screenshot of the error message from the software</a></p>
  
  <p>By adding +1 to mVertices.size() at the very last row, the software works fine. See the example code below.</p>
  
  <p>c)</p>
  
  <pre><code>mVertex vertex;
  vector&lt;mVertex&gt;mVertices;
  for (int i = 0; i &lt; 35; i++)
  {
  vertex.posX = 2.0;
  vertex.posY = 2.0;
  vertex.posZ = 2.0;
  vertex.norX = 2.0;
  vertex.norY = 2.0;
  vertex.norZ = 2.0;
  vertex.U = 0.5;
  vertex.V = 0.5;
  mVertices.push_back(vertex);
  }
  memcpy(msg,                                     // destination
      mVertices.data(),                           //  content  
      (mVertices.size()+1 * sizeof(mVertex)));    // size
  </code></pre>
  
  <p>The code also work, if I remove the for loop.</p>
  
  <p>d)</p>
  
  <pre><code>mVertex vertex;
  vector&lt;mVertex&gt;mVertices;
  
  vertex.posX = 2.0;
  vertex.posY = 2.0;
  vertex.posZ = 2.0;
  vertex.norX = 2.0;
  vertex.norY = 2.0;
  vertex.norZ = 2.0;
  vertex.U = 0.5;
  vertex.V = 0.5;
  mVertices.push_back(vertex);
  
  memcpy(msg,                                     // destination
      mVertices.data(),                           //  content  
      (mVertices.size() * sizeof(mVertex)));      // size
  </code></pre>