Converting (casting?) a dynamic array to a fixed array?

WhatMeWorry via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 4 08:36:20 PDT 2015


Many thanks.  Just to recap, I got the code working with:

glBufferData(GL_ARRAY_BUFFER, (verts.sizeof * verts.length), 
verts.ptr, GL_STATIC_DRAW);

> sizeof on a slice doesn't do what you think it does, it returns 
> the size of the actual slice object I believe.

I have to admit that I didn't understand the above sentence until 
I did the following. Since this is a learning forum, I'll post 
all the details for other learners.


        GLfloat[6] staticVerts  = [  0.0,  1.0,
                                    -1.0, -1.0,
                                     1.0, -1.0 ];

         GLfloat[] dynamicVerts = [  0.0,  1.0,
                                    -1.0, -1.0,
                                     1.0, -1.0 ];

staticVerts.length (num elements) = 6
staticVerts.sizeof (num bytes) = 24
before glBufferData
Press any key to continue . . .
dynamicVerts.length (num elements) = 6
dynamicVerts.sizeof (num bytes) = 8
before glBufferData
Press any key to continue . . .

I see on the site under Array Properties

Static array properties are:
.sizeof	Returns the array length multiplied by the number of 
bytes per array element.

Dynamic array properties are:
.sizeof	Returns the size of the dynamic array reference, which is 
8 in 32-bit builds and 16 on 64-bit builds.


So that was my mistake. But just for arguments sake, wouldn't it 
have been better to define in dynamic array properties a 
.sizeofref and a .sizeof (which behaves like the static 
array.sizeof)?


More information about the Digitalmars-d-learn mailing list