문제

I want to allocate some space for a VBO. The function to do this (in OpenGL) is:

void glBufferData(GLenum  target,
                  GLsizeiptr  size,
                  const GLvoid *  data,
                  GLenum  usage);

In a tutorial i'm reading, the size is calculated by:

N_VERTS*P_SIZE*sizeof *pos,

where N_VERTS is the number of vertices and P_SIZE is the dimension (2D/3D...).

I'm using :

sizeof(pos)

Is there a difference between them? My tests didn't show any difference. Also what does this mean?

sizeof *pos

My guess was that it's equal to

sizeof(pos)/sizeof(float)

but my tests show me it's not.

Edit:

#define N_VERTS 3
#define P_SIZE 2

float pos[N_VERTS*P_SIZE] =
{
    -0.8, -0.8,
    0.8, -0.8,
    0.0, 0.8
};
도움이 되었습니까?

해결책

The expression sizeof *pos is the same as sizeof(*pos), and gives the size of the thing that pos points at. If pos is dynamically allocated, it will give you the size of each element.

So for example:

float *pos = new float[N_VERTS * P_SIZE]; 
// ... fill in pos values 
size = N_VERTS * P_SIZE * sizeof *pos; 

will give the size of the memory allocated for pos in bytes.

In your case, since your data is not dynamically allocated, you could use sizeof pos.

다른 팁

The grammar of the sizeof operator is defined the following way

sizeof unary-expression
sizeof ( type-id )

As any expression can be enclosed in parentheses then

sizeof unary-expression

is equivalent to

sizeof ( unary-expression )

In your example sizeof( *pos ) is the size in bytes of the element of array pos.

Expression *pos is equivalent to pos[0] that is it is the first element of the array.

sizeof( pos ) is the size in bytes of array pos itself.

sizeof( *pos ) is equivalent to sizeof( float ) because sizeof( *pos ) is the size of the element of array pos and each element has type float.

So

sizeof( pos ) / sizeof( float )

is equivalent to

sizeof( pos ) / sizeof( *pos )

Given

float arr[3];
float *p = arr;


sizeof(arr) // gives 3*sizeof(float), i.e. on a compiler using IEEE 754 floats, 12.  
sizeof(p)   // gives just the _pointer_ size, e.g. 4 or 8.
sizeof(*p)  // gives sizeof(float), incidentally 4 as well, assuming IEEE 754 floats.
sizeof(arr[0]) // yields the float size as well, like *p.

sizeof is one of the few places where an array does not decay to a pointer, even if it looks like a function.

My guess is that the tutorial is right; they need the size of your pos array, 6*sizeof(float). Because pos is a true array, this is equal to sizeof(pos), what, I think, you used. Both ways are correct. But I find the tutorial's way safer because you may at some point pass that array around, or allocate it dynamically, and in that case you would suddenly deal with a pointer which has a smaller size while all the rest of the code sees no difference.

In this case, sizeof *pos would only make sense if pos is a pointer to something, in which case sizeof *ptr is a common idiom that gives the size of the referenced (pointed-to) object.

sizeof pos would only return the size of the pointer, ie. not the object size.

edit: after further details have been added to the question, it seems pos is an array, in which case sizeof pos returns the total size of the array (in bytes). however, if the array ever decays into a pointer, sizeof returns the size of an pointer on your system.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top