Cannot allocate a decent amount of memory glBufferData()

Started by
11 comments, last by AbandonedAccount 10 years, 2 months ago

If the commented out line is the one that crashes, that's because you're copying past the end of vertexData. glBufferData (using text replacement with your code) is basically:

boundBuffer[GL_ARRAY_BUFFER] = malloc(10000);

memcpy(boudBuffer[GL_ARRAY_BUFFER], vertexData, 10000);

And assuming vertexData is less than 10000 bytes long, the memcpy part of that crashes after trying to copy past the end of vertexData.

Possible, but why allocating 1000 bytes doesn't crash?

The same reason memcpy doesn't always crash when you copy past your allocated area.

As Hodgeman has already pointed out and as your code clearly shows, you are allocating way to less memory. sizeof(vertexData) returns the size of the datatype of vertexData, which is GLfloat, which is 4 byte. You need sizeof(float)*numVertices*numElementsPerVertex in order allocate a correctly sized buffer.

This would be correct if he dynamically allocated vertexData, but vertexData is a fixed size array. sizeof(float[200]) is 800, not 4. (assuming 4-byte floats and 4-byte pointers)

Advertisement

To summarize, this is how my code should look like?


glBufferData(GL_ARRAY_BUFFER, NUM_VERTS * sizeof(GLfloat) * (ELEM_PER_POS + ELEM_PER_COLOR + ELEM_PER_NORM),
                  vertexData, GL_STATIC_DRAW);

Hide yo cheese! Hide yo wife!

To summarize, this is how my code should look like?


glBufferData(GL_ARRAY_BUFFER, NUM_VERTS * sizeof(GLfloat) * (ELEM_PER_POS + ELEM_PER_COLOR + ELEM_PER_NORM),
                  vertexData, GL_STATIC_DRAW);

It's fine to use sizeof(vertexData) as long as you're not dynamically allocating it (new, malloc, etc.). You will eventually outgrow that when you dynamically load vertex data from files. When you do, you'll basically use sizeof(single element) * number of elements, so it might be "sizeof(vertexData[0]) * vertexData.size()" if you're using stl::vector to store your vertices.

This topic is closed to new replies.

Advertisement