www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - array as parameter

reply "Paul" <aquagnu gmail.com> writes:
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?
Jun 07 2014
next sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
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.
Jun 07 2014
parent "Paul" <aquagnu gmail.com> writes:
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 :)
Jun 07 2014
prev sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
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
Jun 07 2014
parent "Paul" <aquagnu gmail.com> writes:
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!
Jun 07 2014