Reputation:
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
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)
.
std::array
or std::vector
, which carry information about their size.Upvotes: 2
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
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