使用 sizeof(vector[0]) * vector->size() 时出现段错误
鉴于工作代码片段
glBufferData(GL_ARRAY_BUFFER, vertices->size() * sizeof(glm::vec3), &vertices->front(), GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); // Set up our vertex attributes pointer
glGenBuffers(1,&indexBufferId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexBufferId);
// We have to use &triangles.front() otherwise we get vector house keeping garbage
glBufferData(GL_ELEMENT_ARRAY_BUFFER, triangles->size() * sizeof(unsigned), &triangles->front(), GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); // Set up our vertex attributes pointer
基本上我将数据发送到 OpenGL,自然我必须告诉它从哪里复制以及复制多少。在更改向量的类型几次后,我决定不想继续更新每个类型引用并尝试了以下行。
glBufferData(GL_ELEMENT_ARRAY_BUFFER, triangles->size() * sizeof(triangles[0]), &triangles->front(), GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW
使用 sizeof(triangles[0])
而不是 sizeof(unsigned)
。计算嘿,我的向量是类型化的,因此它应该能够在编译时计算元素的类型,这样我就不必在每次重构时都告诉它。
然而事实并非如此。当对任一行及其各自的向量进行此单一更改时,会导致段错误。
另一个令人困惑的地方(在尝试压缩我的代码时出现)是使用
glBufferData(GL_ELEMENT_ARRAY_BUFFER, triangles->size() * sizeof(unsigned), &triangles[0], GL_STATIC_DRAW);
&triangles.front()
替换为 &triangles[0]
会导致相同的段错误。我的理解是这两个陈述应该是相等的。
我的理解错在哪里?我认为出现段错误的语句与有效的语句是等效的。
Given the working code snippet
glBufferData(GL_ARRAY_BUFFER, vertices->size() * sizeof(glm::vec3), &vertices->front(), GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); // Set up our vertex attributes pointer
glGenBuffers(1,&indexBufferId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexBufferId);
// We have to use &triangles.front() otherwise we get vector house keeping garbage
glBufferData(GL_ELEMENT_ARRAY_BUFFER, triangles->size() * sizeof(unsigned), &triangles->front(), GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); // Set up our vertex attributes pointer
basically I am sending data to OpenGL and naturally I have to tell it where to copy from, and how much to copy. After changing types on my vectors a few times I decided I didn't want to keep updating every type reference and tried the following line.
glBufferData(GL_ELEMENT_ARRAY_BUFFER, triangles->size() * sizeof(triangles[0]), &triangles->front(), GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW
using sizeof(triangles[0])
instead of sizeof(unsigned)
. Figuring hey my vector is typed so it should be able to figure the type of an element at compile time so I don't have to tell it each refactor.
However this is not the case. This single change causes a segfault when done to either line with their respective vector.
Another point of confusion (arose when trying to compact my code) is using
glBufferData(GL_ELEMENT_ARRAY_BUFFER, triangles->size() * sizeof(unsigned), &triangles[0], GL_STATIC_DRAW);
replacing &triangles.front()
with &triangles[0]
causes the same segfault. I was of the understanding that these two statements should be equal.
Where is my misunderstanding? I thought the statements which are segfaulting were equivalent to the working ones.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您使用的是 vector->size(),这意味着向量必须是一个指针(除非您使用某些运算符重载魔法)。
尽管向量确实有运算符 [],但向量*[] 的行为不会如您所期望的那样。
在我的机器上,该程序输出以下内容:
换句话说,sizeof(vector[0]) 将为您提供向量的大小,而不是每个向量元素的大小。
由于大小计算错误,因此预计会出现分段错误问题。
Since you are using vector->size(), that means vector must be a pointer(unless you are using some operator overloading magic).
Although vector does have operator [], vector*[] will not behave as you expect it to.
On my machine, that program outputs this:
In other words,
sizeof(vector[0])
will give you the size ofvector
, not the size of each vector element.And since the size calculation is wrong, segmentation fault issues are expected.
您没有显示
triangles
的声明,但从您使用它的方式来看,它似乎是std::vector*
(注意指针)。考虑到这一点,它应该是:并且
You don't show the declaration of
triangles
, but from how you use it it seems to bestd::vector<unsigned>*
(note the pointer). With that in mind, it should be:and