Documentation of D arrays
Sebastian Biallas
groups.5.sepp at spamgourmet.com
Thu Jan 11 17:46:52 PST 2007
BCS wrote:
> Reply to Sebastian,
>
>> Hello!
>>
>> I'm trying to understand array handling in D. Unfortunately the
>> official documentation[1] is not very helpful..
>>
>> [1] http://www.digitalmars.com/d/arrays.html
>>
>> By trial and error I found out that arrays are passed by some COW
>> magic (where is this documentated?). So, if I want to change the
>> content of an array visible for the caller, I have to pass it with an
>> inout-statement (This works, but is it the canonical way?).
>
> Arrays are references types. If you pass an array to a function, the
> function gets a copy of the pointer length pair that the caller uses.
> The function can change the contents of the memory the references but
> can't change the callers reference to that data (unless you use out or
> inout).
Ah, you're right. I guess I have a better picture now.
> As to it seeming to be COW, if you change the length of an array
> sometimes the GC can't extend it in place and moves the whole thing to a
> bigger chunk of ram (this dosn't always happen). When the ~ and ~=
> operators are used, the GC always makes a copy.
Yeah, that's the trick. You can change it in-place (without
inout-statement), and the COW-part happens once you alter the length
(implicitly or explicitly).
I guess what I called COW isn't even the right term.
So, new question: How to I pass T[] to foo(), so that foo() isn't
allowed to change the content of T[]?
More information about the Digitalmars-d
mailing list