   Trouble with C++ and dynamic arrays
  Hey,Im working on some terrain in C++ and it works great so far. However i have the following problem.I declare my indices statically(Must be known before runtime), However i wantto change this into a variable and the only other method is using a vector.When i use a vector my program doesnt render the terrain. So anyhelp please?Thanks:DHere is a small fragment of my code!short indices[(50-1)*(50-1)*6];for (int x=0;x < width-1 ;x++)    {for (int y=0; y < length-1 ;y++)  {indices[(x+y*(width-1))*6+2] = x+y*width;indices[(x+y*(width-1))*6+1] = (x+1)+y*width;indices[(x+y*(width-1))*6] = (x+1)+(y+1)*width;indices[(x+y*(width-1))*6+3] = (x+1)+(y+1)*width;indices[(x+y*(width-1))*6+4] = x+y*width;indices[(x+y*(width-1))*6+5] = x+(y+1)*width;}}device->CreateIndexBuffer((width-1)*(length-1)*6*sizeof(short),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&indexBuffer,NULL);VOID* pIndex;indexBuffer->Lock(0, (width-1)*(length-1)*6*sizeof(short), (void**)&pIndex, 0);memcpy(pIndex,&indices,sizeof(indices));indexBuffer->Unlock();   
  Post the code where you set up the index buffer using a vector.   ;  vector<short> indices((50-1)*(50-1)*6); for (int x=0;x < width-1 ;x++)    {for (int y=0; y < length-1 ;y++)  {indices[(x+y*(width-1))*6+2] = x+y*width;indices[(x+y*(width-1))*6+1] = (x+1)+y*width;indices[(x+y*(width-1))*6] = (x+1)+(y+1)*width;indices[(x+y*(width-1))*6+3] = (x+1)+(y+1)*width;indices[(x+y*(width-1))*6+4] = x+y*width;indices[(x+y*(width-1))*6+5] = x+(y+1)*width;}}device->CreateIndexBuffer((width-1)*(length-1)*6*sizeof(short),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&indexBuffer,NULL);VOID* pIndex;indexBuffer->Lock(0, (width-1)*(length-1)*6*sizeof(short), (void**)&pIndex, 0);memcpy(pIndex,&indices,sizeof(indices));indexBuffer->Unlock();   ;  I think it has to do with the flag with which you create your index buffer.D3DUSAGE_WRITEONLY is used for creating the index buffer in video memory as its fast for reading once its written. Hence, normally static data is placed in it. Whereas for variable data, you can use D3DUSAGE_DYNAMIC.. You can look in MSDN for further knowledge.   ;  Okayyyyyyy i tried device->CreateIndexBuffer((width-1)*(length-1)*6*sizeof(short),D3DUSAGE_DYNAMIC,D3DFMT_INDEX16,D3DPOOL_MANAGED,&indexBuffer,NULL); and for some reason it i get a system.nullreferenceexception   ;  Here:memcpy(pIndex,&indices,sizeof(indices));    ^You are copying the vector. You should copy the data instead.memcpy(pIndex, &indices.front(), indices.size() * sizeof(indices[0]));;Quote:Original post by rozz666Here:memcpy(pIndex,&indices,sizeof(indices));    ^You are copying the vector. You should copy the data instead.memcpy(pIndex, &indices.front(), indices.size() * sizeof(indices[0]));Ahhhhhhhhh works perfectly! Thank you my good man!   ;  okay new problem... I have now used what i have learnt for my vertices however it screws up the programvector<CUSTOMVERTEX> terrainVertices(50*50);for (int x=0;x< width;x++)  {for (int y=0; y< length;y++) {terrainVertices[x+y*width].x=x;terrainVertices[x+y*width].y=0;terrainVertices[x+y*width].z=y;terrainVertices[x+y*width].u=(float)x/30.0f;terrainVertices[x+y*width].v=(float)y/30.0f;}}device->CreateVertexBuffer(sizeof(terrainVertices),0,CUSTOMFVF,D3DPOOL_MANAGED,&vertexBuffer,NULL);VOID* pVoid;vertexBuffer->Lock(0,0,(void**)&pVoid,0);memcpy(pVoid,&terrainVertices.front(),terrainVertices.size() * sizeof(terrainVertices[0]));vertexBuffer->Unlock();//Indices //short indices[(50-1)*(50-1)*6];    vector<short> indices((50-1)*(50-1)*6); for (int x=0;x < width-1 ;x++)    {for (int y=0; y < length-1 ;y++)  {indices[(x+y*(width-1))*6+2] = x+y*width;indices[(x+y*(width-1))*6+1] = (x+1)+y*width;indices[(x+y*(width-1))*6] = (x+1)+(y+1)*width;indices[(x+y*(width-1))*6+3] = (x+1)+(y+1)*width;indices[(x+y*(width-1))*6+4] = x+y*width;indices[(x+y*(width-1))*6+5] = x+(y+1)*width;}}device->CreateIndexBuffer((width-1)*(length-1)*6*sizeof(short),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&indexBuffer,NULL);VOID* pIndex;indexBuffer->Lock(0, (width-1)*(length-1)*6*sizeof(short), (void**)&pIndex, 0); memcpy(pIndex, &indices.front(), indices.size() * sizeof(indices[0]));indexBuffer->Unlock(); I changed the following around device->CreateVertexBuffer(sizeof(terrainVertices),0,CUSTOMFVF,D3DPOOL_MANAGED,&vertexBuffer,NULL);VOID* pVoid;vertexBuffer->Lock(0,0,(void**)&pVoid,0);memcpy(pVoid,&terrainVertices.front(),terrainVertices.size() * sizeof(terrainVertices[0]));Help me once more? Thank you!:);  oh yeah by the way i get System.NullReferenceException for my index buffer now for a wierd reason:S   ;  sizeof(terrainVertices) is the size of the class std::vector<CUSTOMVERTEX>. You want sizeof(CUSTOMVERTEX)*terrainVertices.size(). Just like you do in the memcpy actually.   