I feel the dynamic array .sizeof property is kind of a bait and switch
Mike Parker via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jul 25 19:31:33 PDT 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.
More information about the Digitalmars-d-learn
mailing list