Dynamic arrays with static initialization and maybe a bug with sizeof

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Dec 13 13:39:18 PST 2016


On 12/13/2016 10:27 PM, Xavier Bigand wrote:
>     void    set()
>     {
>         GLfloat[]    data = [
>             -1.0f, -1.0f, 0.0f,
>             1.0f, -1.0f, 0.0f,
>             0.0f,  1.0f, 0.0f,
>         ];
>
>         glBindVertexArray(mVAO);
>         glBufferData(GL_ARRAY_BUFFER, data.sizeof, cast(void*)data,
> GL_STATIC_DRAW);
>     }
>
>
> And I ask my self about the memory management of data, as my data array
> is statically initialized is it allocated on stack?

data is a function-local variable, so there is no static initialization 
going on. The array is allocated on the heap at run-time.

> On another side I have a strange behavior with the sizeof that returns 8
> and not 36 (9 * 4) as I am expecting.

sizeof returns the size of the dynamic array "struct", which is a 
pointer and a length. Instead of sizeof, use .length and multiply with 
the element type's .sizeof: data.length * GLfloat.sizeof


More information about the Digitalmars-d-learn mailing list