Dynamic arrays with static initialization and maybe a bug with sizeof

Xavier Bigand via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Dec 13 13:52:40 PST 2016


Le 13/12/2016 22:39, ag0aep6g a écrit :
> 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


Seems logic, I just read the wrong table on the documentation because 
there is properties static and dynamic arrays are contiguous.

Thanks you


More information about the Digitalmars-d-learn mailing list