digitalmars.D.learn - Dynamic arrays with static initialization and maybe a bug with sizeof
- Xavier Bigand (17/17) Dec 13 2016 Hi,
- ag0aep6g (6/21) Dec 13 2016 data is a function-local variable, so there is no static initialization
- Xavier Bigand (4/28) Dec 13 2016 Seems logic, I just read the wrong table on the documentation because
- Johan Engelen (5/20) Dec 13 2016 Note that if you can define the data array as immutable, you save
- Xavier Bigand (2/26) Dec 13 2016 Thank you for the tips.
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
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
Le 13/12/2016 22:39, ag0aep6g a écrit :On 12/13/2016 10:27 PM, Xavier Bigand wrote:Seems logic, I just read the wrong table on the documentation because there is properties static and dynamic arrays are contiguous. Thanks youvoid 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
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
Le 13/12/2016 23:44, Johan Engelen a écrit :On Tuesday, 13 December 2016 at 21:27:57 UTC, Xavier Bigand wrote:Thank you for the tips.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