Documentation of D arrays

Sebastian Biallas groups.5.sepp at spamgourmet.com
Thu Jan 11 17:32:42 PST 2007


Sean Kelly wrote:
> 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.  

But not (Array *) but (len, byte *), I guess?

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

Hmm, I'm quite sure I can alter the ptr, too (Implicitly, when I append
to the array and there is not enough room).

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

Well, the word "reference" is way to much overloaded. Here you don't
pass the Array (you mentioned above) by reference but the content (the
object ptr points to).

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

Yeah, I guess I understood this already.

Is there something similar to the "const" keyword of C/C++ in D? It
looks a little bit fishy to me, that you can write illegal code in D so
easy.. In C/C++ you can return constant array in way, that the caller
a) knows, it's constant
b) errors are detected at compiler time.

> The above code will actually
> work in Windows because the data segment where string constants are
> stored is not read-only.

For some values of "work" :)

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

But without the COW part?

> 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

Yes, you're right.

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

Hmm, that's not the answer I hoped I'd get :)
It's nice to have a language without suprises, but I could only figure
out that the above part by trying it.

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

That's sad. On a first glance the documentation looks really good, but
then it mostly is about syntax, not about semantic.



More information about the Digitalmars-d mailing list