Are dynamic arrays passed by value? (Was: TList)

Jarrett Billingsley kb3ctd2 at yahoo.com
Wed Mar 19 06:00:40 PDT 2008


"Ary Borenszweig" <ary at esperanto.org.ar> wrote in message 
news:frqsik$1ngj$1 at digitalmars.com...
> Writing that code without "ref" in "add" and "get" didn't work well the 
> first time. I got array index out of bounds. Aren't dynamic arrays always 
> passed by reference?

Yes, but the reference isn't passed by reference ;)

It's the same as something like:

void foo(char* s)
{
    ...
    s = realloc(s, 20);
}

Now s points to a new place in memory.  Yes, the contents pointed to by s 
were passed by reference, but the caller doesn't see the update to s.

In the same way:

void foo(char[] s)
{
    ...
    s ~= "hi!";
}

The local variable 's' is updated in the function, but the caller may not 
see those changes. 




More information about the Digitalmars-d-learn mailing list