'EBO with non-hard-coded element array [duplicate]

I'm trying to create an element/index array that I dynamically build with a set amount of memory. However, my code only works when I hard-code this array. In particular, the following is all the relevant code:

GLfloat elements[15 * 15 * 2 * 3 * 2];
GLuint second_elements[] = {
    0, 16, 17,
    0, 1, 17,
    1, 17, 18,
    1, 2, 18,
    ...
};
GLuint VBO;
glGenBuffers(1, &VBO);

GLuint EBO;
glGenBuffers(1, &EBO);

GLuint VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*)0);
glEnableVertexAttribArray(0);
...
glDrawElements(GL_TRIANGLES, sizeof(elements) / sizeof(elements[0]), GL_UNSIGNED_INT, 0);

When I use elements, my code doesn't render anything. When I use 'second_elements', my code renders everything perfectly. I've confirmed that they hold the same data, and have tried to just render one triangle from each as a test, but I just can't seem to get the EBO to load into VAO properly. Can anyone help?



Solution 1:[1]

Turns out, (for some reason) if I use a std::vector instead of normal heap memory, I can make this work by passing in elements.size() and &elements[0]. I don't know why, but I'll take what I can get.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 p0ptartlov3r