array as parameter

Paul via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 7 14:34:37 PDT 2014


On Saturday, 7 June 2014 at 21:32:08 UTC, Jonathan M Davis via
Digitalmars-d-learn wrote:
> On Sat, 07 Jun 2014 20:56:13 +0000
> Paul via Digitalmars-d-learn 
> <digitalmars-d-learn at puremagic.com> wrote:
>
>> Dynamic array is really reference. Right? But why modification 
>> of
>> parameter in this case does not work:
>>
>> void some_func(string[] s) {
>>   s ~= "xxx"; s ~= "yyy";
>> }
>>
>> but this works:
>>
>> void some_fun(ref string[] s) {
>>   s ~= "xxx"; s ~= "yyy";
>> }
>>
>> In the 1st case s is reference too, is not it?
>
> The first case just slices the array, so it refers to the same 
> data, but the
> slice itself is a different slice, so if you append to it, it 
> doesn't affect
> the original slice, and it could then result in a reallocation 
> so that the two
> slices don't even refer to the same data anymore.
>
> You should read this: http://dlang.org/d-array-article.html
>
> - Jonathan M Davis

Oh, thank you!


More information about the Digitalmars-d-learn mailing list