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

4

3 回答 3

1

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

于 2013-07-02T18:57:29.113 回答
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.

于 2013-07-02T18:58:51.837 回答
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 [].

于 2013-07-02T18:59:07.527 回答