vector大小错误,大于元素数
C++: vector size is wrong and higher than the number of elements
在我尝试加载.OBJ
-文件时,我将顶点数据加载到std::vectors
中,以便稍后将它们发送给GPU。我填充了三个向量,分别是法线、顶点和纹理坐标。法向量的大小远远大于其他两个向量的大小,即使它被相同数量的元素填充。
SceneTree* generateSceneTree(OBJScene* scene){
PostProcessing::triangulateFaces(scene);
SceneNode* node = new SceneNode;
vector<Vec<3>>& sceneNormals = scene->attributeData[Attribute::Normal];
vector<Vec<3>>& sceneVertices = scene->attributeData[Attribute::Position];
vector<Vec<3>>& sceneTexCoords = scene->attributeData[Attribute::TexCoord];
map<string,MaterialInfo*> mtls;
for(string s : scene->mtlLibs){
auto temp = loadMTL(s);
mtls.insert(temp.begin(),temp.end());
}
vector<Vec<3>> meshNormals; <-- creating vectors here.
vector<Vec<3>> meshVertices;
vector<Vec<2>> meshTexCoords;
for(auto g : scene->groups){
meshNormals.clear();
meshNormals.reserve(g.faces.size()*3);
meshVertices.clear();
meshVertices.reserve(g.faces.size()*3);
meshTexCoords.clear();
meshTexCoords.reserve(g.faces.size()*3);
AABB bBox;
cout << "num of faces: " << g.faces.size() << endl;
for(auto f : g.faces){
for(auto p : f.points){
uint vIndex = p.indices[Attribute::Position];
uint nIndex = p.indices[Attribute::Normal];
uint tIndex = p.indices[Attribute::TexCoord];
Vec<3> n = sceneNormals.at(nIndex);
Vec<3> v = sceneVertices.at(vIndex);
Vec<3> t = sceneTexCoords.at(tIndex);
meshNormals.push_back(n);
meshVertices.push_back(v);
meshTexCoords.push_back(t.toVec<2>());
bBox += meshVertices.back();
}
}
cout << "meshNormals size: " << meshNormals.size() << endl;
cout << "meshVertices size: " << meshVertices.size() << endl;
cout << "meshTexCoords size: " << meshTexCoords.size() << endl;
Mesh* m = new Mesh({
{meshVertices,Attribute::Position},
{meshNormals,Attribute::Normal},
{meshTexCoords,Attribute::TexCoord}
},GL_TRIANGLES);
SceneLeaf* leaf = new SceneLeaf;
leaf->nodeData = {Matrix4(),bBox};
leaf->leafData = {m, mtls[g.mtlName]};
node->addChild(leaf);
}
return node;
}
输出:num of faces: 1087474
meshNormals size: 2958875950
meshVertices size: 3262422
meshTexCoords size: 3262422
这看起来非常不合逻辑。程序崩溃之后与std::bad_array_new_length
异常,因为Mesh
类不能创建一个大小为2958875950
的数组发送给GPU。
更新:
如果我交换meshVertices
和meshNormals
的声明,meshVertices
有错误的大小。因此,第一个创建的向量受到影响。
如果我用std::list
代替std::vector
,一切都工作。
如果我注释掉....reserve(g.faces.size()*3);
行,则抛出std::bad_alloc
。
我的猜测是,你有一个内存损坏的错误"某处",是覆盖meshNormals
变量的堆栈。交换meshNormals
和meshVertices
声明导致meshVertices
变坏的事实与该理论相匹配。
要缩小问题的范围,你可以做以下几件事:
- 注释掉内部
for(auto p : f.points)
循环中的所有行,看看错误是否仍然发生。 - 假设没有,开始逐行取消注释,直到错误再次出现。
- 试着做一个最小的,独立的测试代码的例子,重复的问题(这将有助于极大,如果你这样做之前张贴一个问题)。
简单的答案是,你在开始的指针指向其他地方,要么:
- 你正在铸造"场景"结构。
- 你有垃圾或未设置指针在场景结构的法向量。可能是你在进入函数之前就把垃圾放在那里了。
你有没有注意到2958875950是垃圾/负整数?
相关文章:
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- 在向量中查找大于 0(或通常为 k)的最小元素的最佳方法是什么?
- 第 k 个最小元素 - 不能创建大于 k 大小的数组
- 如何在随机数列表中查找大于或小于的元素
- C++:检查向量中的元素是否大于另一个具有相同索引的元素的有效方法?
- 计算向量中大于数字的元素
- (也许是NP-Hard)求一个集合的子集总数,使得每个子集在与其所有元素相乘时的值都大于X
- 如何在向量中找到大于某些数字的所有元素
- 访问 std::string 中的元素,其中字符串的位置大于其大小
- 将大于k的数组元素导入到另一个数组C 中
- 使用std :: find_if with std :: vector找到大于某些值的最小元素
- 对大于属性的元素的矢量对象进行二进制搜索
- 为什么在 c++ 中计算重复元素时,我的计数值大于数组中的元素数
- 矢量排序算法,仅对大于 0 的元素进行排序
- 获取所有矢量元素大于值C
- 获取所有大于某个值的 stl 向量元素
- std::map的大小大于调试器中显示的元素数量
- 能够访问索引大于数组长度的元素
- 如何检查数组的所有元素是否都大于零C++
- 如何找到映射中key大于val的第一个元素