sizeof instance of std::vector returns wrong number of elements in the vector

I have a cycle, that defines a vector of GLfloat vertices coordinates. (three 1.0f floats describe the color, it doesn’t matter)

std::vector<GLfloat> verticesUnitPoints;

float xCurrent = -1.0f;

for (int i = 0; i <= 8; i++)
{
    float yOffset = 0.01f;

            //first vertex
    verticesUnitPoints.push_back(xCurrent);
    verticesUnitPoints.push_back(yOffset);
    verticesUnitPoints.push_back(0.0f);

    verticesUnitPoints.push_back(1.0f);
    verticesUnitPoints.push_back(1.0f);
    verticesUnitPoints.push_back(1.0f);

            //second vertex
    verticesUnitPoints.push_back(xCurrent);
    verticesUnitPoints.push_back(-yOffset);
    verticesUnitPoints.push_back(0.0f);

    verticesUnitPoints.push_back(1.0f);
    verticesUnitPoints.push_back(1.0f);
    verticesUnitPoints.push_back(1.0f);

    xCurrent += 0.25f;
}

I also have a glBufferData function, that must create a buffer with enough place for all vector elements.

glBufferData(GL_ARRAY_BUFFER, sizeof(verticesUnitPoints) * sizeof(GLfloat), &verticesUnitPoints[0], GL_STATIC_DRAW);

But as I can see in output window, only small part of all vertices is rendered.

I can easily fix it by just increasing the multiplier of sizeof(verticesUnitPoints) but I find this solution horrible.

>Solution :

sizeof(verticesUnitPoints) returns size of std::vector class (not instance), which is fixed for any number of elements.

In order to obtain this number use member function std::vector::size (verticesUnitPoints.size())

Leave a Reply