Documentation of D arrays

Sean Kelly sean at f4.ca
Thu Jan 11 17:13:08 PST 2007


Sebastian Biallas wrote:
> 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?).

Almost.  Dynamic arrays are declared internally like so in D:

struct Array
{
     size_t len;
     byte*  ptr;
}

So passing a dynamic array by value is essentially the same as passing 
around a pointer.  The only effect adding 'inout' to your function will 
have is that the length of the array can be altered and those changes 
will persist when the function completes.

There is a brief mention of this in:

http://www.digitalmars.com/d/function.html

"For dynamic array and object parameters, which are passed by reference, 
in/out/inout apply only to the reference and not the contents."

> Next question: How can I initialize an array?
> It seems like COW works only for parameters. Eg.
> 
> void foo(inout char[] s)
> {
>         s = "blub";
> }
> void bar()
> {
> 	char[] s;
> 	foo(s);
> 	s[1] = 'a'; // will crash
> }

Doing:

     s = "blurb";

allocates no memory, but rather just changes Array.ptr to point to 
"blurb" and sets Array.len appropriately.  The above code will actually 
work in Windows because the data segment where string constants are 
stored is not read-only.

> So, how can I copy the string "blub" into s? s[] = "blub" doesn't work
> because the .length won't be adjusted.

     s = "blurb".dup;

> Oh, while writing this I noticed "blub".dup does work. It this the
> preferred way or should I manually alter the .length?

Yes :-)

> So what exactly is T[]? According to the documentation it's a tuple
> (pointer, length). So, if I pass a T[] to a function, pointer and length
> are passed by value (unless I specify and (in)out statement)? Is this
> some array magic or can I use this for own types?

See above.  You could duplicate this in your own code by creating a 
struct containing pointers.  Also, I don't think it's a good idea to 
call T[] a Tuple in D because the term has a fairly specific 
connotation.  See the section entitled "Tuple Parameters" at 
http://www.digitalmars.com/d/template.html and also 
http://www.digitalmars.com/d/phobos/std_typetuple.html

> I also found out that I can write
> void foo(inout int[] a)
> {
> 	a ~= 1;
> }
> So "~=" does not only support T[] as RHS but also T. Where is the
> documentation for this?

http://www.digitalmars.com/d/arrays.html I suppose, though the 
description isn't explicit.  Rather, it's implied by "the ~= operator 
means append."

> Sorry, if these are obvious questions, but I can't figure this out by
> the official documentation (or I'm blind).

Not at all.  I've been using D for a few years now, and I still have 
trouble finding things in the spec.  It's pretty much all there, but not 
always in the most obvious location.


Sean



More information about the Digitalmars-d mailing list