Passing dynamic arrays
spir
denis.spir at gmail.com
Mon Nov 8 13:54:28 PST 2010
On Mon, 08 Nov 2010 15:32:56 -0500
Jesse Phillips <jessekphillips+D at gmail.com> wrote:
> But they are past by reference. You can modify the data all you want, but cannot reassign the reference itself.
No, they are _not_ passed by reference. Stop saying that, this is precisely what causes confusion. This is reference semantics:
class C {
int* ints;
}
void main () {
auto c = new C();
auto d = c;
auto ints = [1,2,3];
c.ints = &(ints[0]);
assert(c.ints == d.ints);
assert(*(c.ints) == *(d.ints));
}
Whatever change one performs on object fields never breaks the relation with other vars pointing to the same object; because the object itself is referenced.
D arrays do not work that way: the kind of struct is *copied*, not referenced; arrays themselves are *values*. So that changes to the content that requires reallocation breaks the relation.
(Additional confusion is brought by the fact that, if a is an array, after "b=a" (b is a) yields true, for any reason, but this is also wrong. I guess 'is' is overloaded for arrays to compare the adresses of the contents instead of the ones of the array-structs, but this is misguided.)
Denis
-- -- -- -- -- -- --
vit esse estrany ☣
spir.wikidot.com
More information about the Digitalmars-d
mailing list