www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Dynamic arrays with static initialization and maybe a bug with sizeof

reply Xavier Bigand <flamaros.xavier gmail.com> writes:
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.
Dec 13 2016
next sibling parent reply ag0aep6g <anonymous example.com> writes:
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
Dec 13 2016
parent Xavier Bigand <flamaros.xavier gmail.com> writes:
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
Dec 13 2016
prev sibling parent reply Johan Engelen <j j.nl> writes:
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
Dec 13 2016
parent Xavier Bigand <flamaros.xavier gmail.com> writes:
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.
Dec 13 2016