user4254528
user4254528

Reputation:

sizeof array struct error

I'm trying to make function that initializes VAO on specif index, with some data. The problem is, when I'm accessing the sizeof vertices, return's an wrong size.

Here is the data:

typedef struct {
    GLfloat XYZW[4];
    GLfloat RGBA[4];
} Vertex;

const Vertex Vertices2[] =
{
    { { 0.25f, 0.25f, 0.0f, 1.0f },{ 1.0f, 0.0f, 0.0f, 1.0f } },
    { { 0.75f, 0.25f, 0.0f, 1.0f },{ 0.0f, 1.0f, 0.0f, 1.0f } },
    { { 0.50f, 0.75f, 0.0f, 1.0f },{ 0.0f, 0.0f, 1.0f, 1.0f } }
};
const Vertex Indices2[] = {....}

I call the function like this:

createArrayObjects(0, Vertices2, Indices2);

void createArrayObjects(int index, const Vertex vertices[], const GLubyte indices[]){
    cout << sizeof(vertices) << endl;  ---> returns 4
    cout << sizeof(Vertices2) << endl; ---> returns 96
...
}

If I use sizeof(Vertices2), to fill the VBO, the program runs fine. Without the correct size on the input vertices, I can't fill the VAO and VBO and visualize correctly the data.

Upvotes: 0

Views: 137

Answers (3)

Emil Laine
Emil Laine

Reputation: 42828

In C++, function parameters of type any_type[] are treated by the compiler as any_type*.

So the type of vertices is really a pointer, and you're getting the size of a pointer with sizeof(vertices).

  • If you need to know the size of the array you pass to a function, you can pass its size as another parameter.
  • Or you can do as vsoftco said, in which case the function template will be instantiated for each different array size you pass to the function.
  • If you want to be "modern" you could also pass an std::array or std::vector, which carry information about their size.

Upvotes: 2

Julian
Julian

Reputation: 2907

The parameter const Vertext vertices[] you're passing in to the function is essentially a pointer, that's why it's showing up as size 4. You'll have to communicate the size of the array to your function some other way such as by passing in the size as a separate parameter.

You can find more detail in a similar question asked previously here.

Upvotes: 1

vsoftco
vsoftco

Reputation: 56547

Any time you pass an array to a function by value, it decays to a pointer, regardless of the fact that you pass it as arr[] or arr[size], so the size reported by sizeof will be that of a pointer, i.e. in general 4 or 8 bytes. If you want to pass an array and "detect" its size, you can pass it by reference via a template non-type parameter, like in the following simple example:

#include <iostream>

template<typename T, int N>
void f(T(&arr)[N])
{
    std::cout << "Size: " << N;
}

int main()
{
    double arr[10];
    f(arr); // outputs 10
}

Upvotes: 1

Related Questions