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
  • 8,400
  • 16
  • 48
  • 74

3 Answers3

1

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

NPE
  • 464,258
  • 100
  • 912
  • 987
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
  • 198,982
  • 21
  • 284
  • 478
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
  • 15,325
  • 2
  • 22
  • 37