使用 sizeof(vector[0]) * vector->size() 时出现段错误

发布于 2024-12-13 18:35:37 字数 1620 浏览 0 评论 0原文

鉴于工作代码片段

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心清如水 2024-12-20 18:35:37

由于您使用的是 vector->size(),这意味着向量必须是一个指针(除非您使用某些运算符重载魔法)。

尽管向量确实有运算符 [],但向量*[] 的行为不会如您所期望的那样。

#include <vector>
#include <iostream>

using std::vector;
using std::cout;
using std::endl;

int main() {
    vector<int>* x = new vector<int>();
    x->push_back(3);
    x->push_back(4);

    cout << sizeof(vector<int>*) << endl;
    cout << sizeof(x) << endl;
    cout << sizeof(vector<int>) << endl;
    cout << sizeof(*x) << endl;
    cout << sizeof(x[0]) << endl;
    cout << sizeof(int) << endl;
    cout << sizeof((*x)[0]) << endl;

    return 0;
}

在我的机器上,该程序输出以下内容:

8
8
24
24
24
4
4

换句话说,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.

#include <vector>
#include <iostream>

using std::vector;
using std::cout;
using std::endl;

int main() {
    vector<int>* x = new vector<int>();
    x->push_back(3);
    x->push_back(4);

    cout << sizeof(vector<int>*) << endl;
    cout << sizeof(x) << endl;
    cout << sizeof(vector<int>) << endl;
    cout << sizeof(*x) << endl;
    cout << sizeof(x[0]) << endl;
    cout << sizeof(int) << endl;
    cout << sizeof((*x)[0]) << endl;

    return 0;
}

On my machine, that program outputs this:

8
8
24
24
24
4
4

In other words, sizeof(vector[0]) will give you the size of vector, not the size of each vector element.
And since the size calculation is wrong, segmentation fault issues are expected.

七秒鱼° 2024-12-20 18:35:37

您没有显示 triangles 的声明,但从您使用它的方式来看,它似乎是 std::vector* (注意指针)。考虑到这一点,它应该是:

sizeof( (*triangles)[0] )

并且

&(*triangles)[0]

You don't show the declaration of triangles, but from how you use it it seems to be std::vector<unsigned>* (note the pointer). With that in mind, it should be:

sizeof( (*triangles)[0] )

and

&(*triangles)[0]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文