Passing dynamic arrays

Daniel Gibson metalcaedes at gmail.com
Mon Nov 8 17:14:17 PST 2010


On Tue, Nov 9, 2010 at 1:24 AM, Jesse Phillips
<jessekphillips+D at gmail.com> wrote:
>
> The array-struct is the reference, so it is what gets compared. That means both the internal pointer and length must be the same. Just because the reference is more than an address does not make it any less a reference.
>

Unlike in C, a D array is more than a reference.
An array is the data (or a reference to its data) + metadata (its
length) - the metadata belongs to the array (and not to the
reference).
This means that, when you say "the array is passed by reference" one
expects that also the arrays length is passed by reference - because
the length belongs to the array.

>
> The distinction is that an Array can have its reference changed by resizing (which is not an option for other reference types).
>

If that is not an option for any other reference type, why should it
be an option for arrays? That is just inconsistent and doesn't make
any sense.

No, Arrays should not be considered reference types when passed to a function.
As someone else said before: Logically you don't pass the array but a
slice that contains the whole array.

Cheers,
- Daniel


More information about the Digitalmars-d mailing list