Thread overview
Dynamic arrays with static initialization and maybe a bug with sizeof
Dec 13, 2016
Xavier Bigand
Dec 13, 2016
ag0aep6g
Dec 13, 2016
Xavier Bigand
Dec 13, 2016
Johan Engelen
Dec 13, 2016
Xavier Bigand
December 13, 2016
Hi,

I have the following code snippet :
	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?

On another side I have a strange behavior with the sizeof that returns 8 and not 36 (9 * 4) as I am expecting.
December 13, 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
December 13, 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
December 13, 2016
On Tuesday, 13 December 2016 at 21:27:57 UTC, Xavier Bigand wrote:
> Hi,
>
> I have the following code snippet :
> 	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?

Note that if you can define the data array as immutable, you save on heap memory allocation + copying (LDC, from -O0):
https://godbolt.org/g/CNrZR7

-Johan

December 14, 2016
Le 13/12/2016 23:44, Johan Engelen a écrit :
> On Tuesday, 13 December 2016 at 21:27:57 UTC, Xavier Bigand wrote:
>> Hi,
>>
>> I have the following code snippet :
>>     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?
>
> Note that if you can define the data array as immutable, you save on
> heap memory allocation + copying (LDC, from -O0):
> https://godbolt.org/g/CNrZR7
>
> -Johan
>

Thank you for the tips.