Documentation of D arrays

BCS ao at pathlink.com
Thu Jan 11 17:27:58 PST 2007


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). 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.

[...]
> the official documentation (or I'm blind).

I offten feel that way myself. I have had so much trouble finding things 
that /were/ put in a good place that I have a CGI sript on my box that gives 
me a grep of the whole D spec converted into a webpage with links and everything.

> 
> Regards,
> Sebastian





More information about the Digitalmars-d mailing list