Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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())

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading