Thread overview
I feel the dynamic array .sizeof property is kind of a bait and switch
Jul 26, 2017
WhatMeForget
Jul 26, 2017
Mike Parker
Jul 26, 2017
WhatMeWorry
Jul 27, 2017
Mike Parker
Jul 26, 2017
Adam D. Ruppe
Jul 26, 2017
WhatMeWorry
July 26, 2017
Static Arrays have property
.sizeof which returns the array length multiplied by the number of bytes per array element.

Dynamic Arrays have property
.sizeof which returns the size of the dynamic array reference, which is 8 in 32-bit builds and 16 on 64-bit builds.

Why not have dynamic arrays with a .sizeof identical to static arrays and say another property called .sizeref which handles the 32 or 64 bit references.

Maybe Phobos has something that I'm not aware of?

I've hand rolled a function which is working for me currently, but with my coding ability, I'd feel much safer with something official :)

It just seems like something this basic regarding dynamic arrays should just be built-in.












July 26, 2017
On Wednesday, 26 July 2017 at 02:24:06 UTC, WhatMeForget wrote:
> Static Arrays have property
> .sizeof which returns the array length multiplied by the number of bytes per array element.
>
> Dynamic Arrays have property
> .sizeof which returns the size of the dynamic array reference, which is 8 in 32-bit builds and 16 on 64-bit builds.
>
> Why not have dynamic arrays with a .sizeof identical to static arrays and say another property called .sizeref which handles the 32 or 64 bit references.
>
> Maybe Phobos has something that I'm not aware of?
>
> I've hand rolled a function which is working for me currently, but with my coding ability, I'd feel much safer with something official :)
>
> It just seems like something this basic regarding dynamic arrays should just be built-in.

Because .sizeof has nothing to do with how many elements are in the array. It tells you how much space the array itself takes up. With static arrays, the memory for the elements if part of the array itself, so it is counted in the size. For dynamic arrays, it is not. For .sizeof to report the size of the allocated memory would be incorrect.
July 26, 2017
On Wednesday, 26 July 2017 at 02:24:06 UTC, WhatMeForget wrote:
> Static Arrays have property
> .sizeof which returns the array length multiplied by the number of bytes per array element.
>
> Dynamic Arrays have property
> .sizeof which returns the size of the dynamic array reference, which is 8 in 32-bit builds and 16 on 64-bit builds.

Both actually already do exactly the same thing: .sizeof returns the size of the variable. Same thing with pointers, class references, and everything else.

> I've hand rolled a function which is working for me currently, but with my coding ability, I'd feel much safer with something official :)

You could also do (cast(ubyte[]) array).length.

> It just seems like something this basic regarding dynamic arrays should just be built-in.

What are you using it for?
July 26, 2017
On Wednesday, 26 July 2017 at 02:31:33 UTC, Mike Parker wrote:
> On Wednesday, 26 July 2017 at 02:24:06 UTC, WhatMeForget wrote:
>> [...]
>
> Because .sizeof has nothing to do with how many elements are in the array. It tells you how much space the array itself takes up.

Totally agree.  .length returns the the number of array elements.

> With static arrays, the memory for the elements if part of the array itself, so it is counted in the size. For dynamic arrays, it is not. For .sizeof to report the size of the allocated memory would be incorrect.

OK, Then I assume the critical thing is that dynamic arrays memory is
not part of the array itself.  But is this a deal breaker?

July 26, 2017
On Wednesday, 26 July 2017 at 02:32:07 UTC, Adam D. Ruppe wrote:
>
>> I've hand rolled a function which is working for me currently, but with my coding ability, I'd feel much safer with something official :)
>
> You could also do (cast(ubyte[]) array).length.
>

This was my (way over complicated) attempt at the same thing. I'll blame my verbosity because I was trying to teach myself about templates and constraints at the time :)

/+
There is a special type of array which acts as a wildcard that can hold arrays of any kind, declared as void[].  The .length of a void array is the length of the data in
bytes rather than the number of elements in its original type. "
+/

int arrayByteSize(T)(T someArray) if (isDynamicArray!(T))
{
    ubyte[] arr = cast(ubyte[]) someArray;
    return cast(int) arr.length;
}

>> It just seems like something this basic regarding dynamic arrays should just be built-in.
>
> What are you using it for?

glBufferData(GL_ARRAY_BUFFER, vertices.arrayByteSize, vertices.ptr, GL_STATIC_DRAW);

I find that openGL uses array buffers all over the place.

I just keep going back to the idea that such low level functionality should be inherent in either the language or Phobos.  If that is even possible.
July 27, 2017
On Wednesday, 26 July 2017 at 16:27:57 UTC, WhatMeWorry wrote:
> On Wednesday, 26 July 2017 at 02:31:33 UTC, Mike Parker wrote:
>
>> With static arrays, the memory for the elements if part of the array itself, so it is counted in the size. For dynamic arrays, it is not. For .sizeof to report the size of the allocated memory would be incorrect.
>
> OK, Then I assume the critical thing is that dynamic arrays memory is
> not part of the array itself.  But is this a deal breaker?

A deal breaker for what? For making sizeof return the amount of memory allocated? Yes. It's the same behavior in C and C++:

float verts[3];
assert(sizeof(verts) == (sizeof(float) * 3));

float *verts = malloc(sizeof(float)*3);
assert(sizeof(verts) == sizeof(void*));