Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
June 07, 2014 array as parameter | ||||
---|---|---|---|---|
| ||||
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? |
June 07, 2014 Re: array as parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On Saturday, 7 June 2014 at 20:56:14 UTC, Paul 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?
It's a value type that holds a reference to data. If you modify the *actual* slice, rather than the referenced items, then you need to pass by ref.
|
June 07, 2014 Re: array as parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On Sat, 07 Jun 2014 20:56:13 +0000 Paul via Digitalmars-d-learn <digitalmars-d-learn@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 |
June 07, 2014 Re: array as parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On Saturday, 7 June 2014 at 21:17:41 UTC, monarch_dodra wrote:
> On Saturday, 7 June 2014 at 20:56:14 UTC, Paul 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?
>
> It's a value type that holds a reference to data. If you modify the *actual* slice, rather than the referenced items, then you need to pass by ref.
Seems that string[] is not real reference as in C++. As I understand there is only one array of strings. And when I try to modify it in function with ref or whithout - array is the same and should be modified. Or there is hidden copy of this array? Or there is some policy in reference semantic - modifiable dynamic array and read only - when it is copying in stack as function argument :)
|
June 07, 2014 Re: array as parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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@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!
|
Copyright © 1999-2021 by the D Language Foundation