0

I'm trying to write a OBJMesh loader at the moment in DirectX and I came across a problem with a section of my code:

unsigned int vertexCount = vertexData.size();
VERTEX* vertices = new VERTEX[vertexCount];
std::copy(vertexData.begin(), vertexData.end(), vertices);

The vertexData in the std::copy is a vector<VERTEX> and I'm trying to copy the data in vertexData to my newly created vertices array.

when I load in my objmesh file, I have checked there are 2841 vertices which is correct and I've stored it to vertexCount (I've checked it by doing a std::cout << vertexCount).

However, the real problem is that when I check the data and size of the array by entering std::cout << vertices[3000].x it prints out something without triggering the index out of bound error.

Knowing I've created the vertices array with a size of 2841, the compiler should stop and display a error should it not? What exactly is the problem and why is it behaving like this??

Please help

EDIT: using Visual Studio 2010 Windows 7 64bit

Danny
  • 9,199
  • 16
  • 53
  • 75

3 Answers3

1

The behaviour of vertices[3000].x is undefined. The compiler/runtime are not obliged to provide any diagnostic.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Accessing an element beyond the end of an array in C++ is undefined behavior. That basically means that anything can happen and that you should not be expecting any particular result or error.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0

What you do leads to undefined behavior. anything can happen, you can absolutely not expect out of bounds exception. Which you could realistically get only if used std::vector and accessed elements using .at() instead of [].

Balog Pal
  • 16,195
  • 2
  • 23
  • 37