C++ 索引超出范围在它应该发生的时候没有发生 [英] C++ index out of range NOT occurring when it should
问题描述
我目前正在尝试在 DirectX 中编写一个 OBJMesh 加载器,但我的一段代码遇到了问题:
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);
std::copy
中的 vertexData
是一个 vector
,我试图将 vertexData 中的数据复制到我新创建的顶点数组.
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.
当我加载 objmesh 文件时,我检查了 2841 个正确的顶点,并将其存储到 vertexCount(我通过执行 std::cout << vertexCount
).
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
).
然而,真正的问题是,当我通过输入 std::cout << 来检查数组的数据和大小时.vertices[3000].x
它打印出一些东西而不会触发索引越界错误.
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.
知道我已经创建了大小为 2841 的顶点数组,编译器应该停止并显示错误吗?究竟是什么问题,为什么会这样??
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??
请帮忙
使用 Visual Studio 2010视窗 7 64 位
using Visual Studio 2010 Windows 7 64bit
推荐答案
vertices[3000].x
的行为是 未定义.编译器/运行时没有义务提供任何诊断.
The behaviour of vertices[3000].x
is undefined. The compiler/runtime are not obliged to provide any diagnostic.
这篇关于C++ 索引超出范围在它应该发生的时候没有发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!