title: Odd access violation reading error / glDrawArrays usage
body: <p>I'm not sure if this belongs here or in graphichs progamming.....I am getting a really annoying access violation reading error and I can't figure out why. What I am trying to do is to refactor a keyframing function (calculate the mid position between two vertices positions). This function compiles and works fine</p>
  
  <pre><code>glBegin(GL_TRIANGLES);
      for(int i = 0; i &lt; numTriangles; i++) {
          MD2Triangle* triangle = triangles + i;
          for(int j = 0; j &lt; 3; j++) {
              MD2Vertex* v1 = frame1-&gt;vertices + triangle-&gt;vertices[j];
              MD2Vertex* v2 = frame2-&gt;vertices + triangle-&gt;vertices[j];
              Vec3f pos = v1-&gt;pos * (1 - frac) + v2-&gt;pos * frac;
              Vec3f normal = v1-&gt;normal * (1 - frac) + v2-&gt;normal * frac;
              if (normal[0] == 0 &amp;&amp; normal[1] == 0 &amp;&amp; normal[2] == 0) {
                  normal = Vec3f(0, 0, 1);
              }
              glNormal3f(normal[0], normal[1], normal[2]);
  
              MD2TexCoord* texCoord = texCoords + triangle-&gt;texCoords[j];
              glTexCoord2f(texCoord-&gt;texCoordX, texCoord-&gt;texCoordY);
              glVertex3f(pos[0], pos[1], pos[2]);
          }
      }
  glEnd();
  </code></pre>
  
  <p>Here the function calculates the positions and draws them. What I'd like to do is to calculate all positions before hand, store them in a Vertex array and then draw them.
  If I try and remove it and replace this bloc in the exact same part of the program with the following</p>
  
  <pre><code>int vCount = 0;
      for(int i = 0; i &lt; numTriangles; i++) {
          MD2Triangle* triangle = triangles + i;
          for(int j = 0; j &lt; 3; j++) {
              MD2Vertex* v1 = frame1-&gt;vertices + triangle-&gt;vertices[j];
              MD2Vertex* v2 = frame2-&gt;vertices + triangle-&gt;vertices[j];
              Vec3f pos = v1-&gt;pos * (1 - frac) + v2-&gt;pos * frac;
              Vec3f normal = v1-&gt;normal * (1 - frac) + v2-&gt;normal * frac;
              if (normal[0] == 0 &amp;&amp; normal[1] == 0 &amp;&amp; normal[2] == 0) {
                  normal = Vec3f(0, 0, 1);
              }
  
              indices[vCount] = normal[0];
              vCount++;
              indices[vCount] = normal[1];
              vCount++;
              indices[vCount] = normal[2];
              vCount++;
  
              MD2TexCoord* texCoord = texCoords + triangle-&gt;texCoords[j];
              indices[vCount] = texCoord-&gt;texCoordX;
              vCount++;
              indices[vCount] = texCoord-&gt;texCoordY;
              vCount++;
  
              indices[vCount] = pos[0];
              vCount++;
              indices[vCount] = pos[1];
              vCount++;
              indices[vCount] = pos[2];
              vCount++;
          }
  
      }
  </code></pre>
  
  <p>I get access violation error "Unhandled exception at 0x01455626 in Graphics_template_1.exe: 0xC0000005: Access violation reading location 0xed5243c0" pointing at line 7</p>
  
  <pre><code>Vec3f pos = v1-&gt;pos * (1 - frac) + v2-&gt;pos * frac;
  </code></pre>
  
  <p>where the two Vs seems to have no value in the debugger.... Till this point the function behaves in exactly the same way as the one above, I don't understand why this happens?</p>
  
  <p>EDIT ------------------------------------------------------------------------------------</p>
  
  <p>Thank you Werner for spotting that the issue was the array initialization! As per your advice I refactored the function using std:vector containers and made the drawing use glDrawArrays instead of immediate mode.... But instead of an improvement in performance the framerate is a lot lower than before! Am I using this function right/efficiently? This is the refactored draw function:</p>
  
  <pre><code>    for(int i = 0; i &lt; numTriangles; i++) {
          MD2Triangle* triangle = triangles + i;
          for(int j = 0; j &lt; 3; j++) {
              MD2Vertex* v1 = frame1-&gt;vertices + triangle-&gt;vertices[j];
              MD2Vertex* v2 = frame2-&gt;vertices + triangle-&gt;vertices[j];
              Vec3f pos = v1-&gt;pos * (1 - frac) + v2-&gt;pos * frac;
              Vec3f normal = v1-&gt;normal * (1 - frac) + v2-&gt;normal * frac;
              if (normal[0] == 0 &amp;&amp; normal[1] == 0 &amp;&amp; normal[2] == 0) {
                  normal = Vec3f(0, 0, 1);
              }
  
  
              normals.push_back(normal[0]);
              normals.push_back(normal[1]);
              normals.push_back(normal[2]);
  
              MD2TexCoord* texCoord = texCoords + triangle-&gt;texCoords[j];
              textCoords.push_back(texCoord-&gt;texCoordX);
              textCoords.push_back(texCoord-&gt;texCoordY);
  
              vertices.push_back(pos[0]);
              vertices.push_back(pos[1]);
              vertices.push_back(pos[2]);
          }
  
      }
  
  
      glEnableClientState(GL_NORMAL_ARRAY);
      glEnableClientState(GL_TEXTURE_COORD_ARRAY);
      glEnableClientState(GL_VERTEX_ARRAY);
  
      glNormalPointer(GL_FLOAT, 0, &amp;normals[0]);
      glTexCoordPointer(2, GL_FLOAT, 0, &amp;textCoords[0]); 
      glVertexPointer(3, GL_FLOAT, 0, &amp;vertices[0]);
  
  
  
      glDrawArrays(GL_TRIANGLES, 0, vertices.size()/3);
  
  
      glDisableClientState(GL_VERTEX_ARRAY);  // disable vertex arrays
      glDisableClientState(GL_TEXTURE_COORD_ARRAY);
      glDisableClientState(GL_NORMAL_ARRAY);
  
      vertices.clear();
      textCoords.clear();
      normals.clear();
  </code></pre>
  
  <p>Is there something extra that I am doing here? Cos this is really meant to be more efficient that glBegin()/End(), right?</p>
  
  <p>Thanks for your time and help!</p>