digitalmars.D.learn - array as parameter
- Paul (10/10) Jun 07 2014 Dynamic array is really reference. Right? But why modification of
- monarch_dodra (4/14) Jun 07 2014 It's a value type that holds a reference to data. If you modify
- Paul (8/26) Jun 07 2014 Seems that string[] is not real reference as in C++. As I
- Jonathan M Davis via Digitalmars-d-learn (8/18) Jun 07 2014 The first case just slices the array, so it refers to the same data, but...
- Paul (3/30) Jun 07 2014 Oh, thank you!
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
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
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: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 :)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
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
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:Oh, thank you!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