Set null as function array parameter

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 12 09:17:40 PST 2015


On Monday, 12 January 2015 at 16:32:31 UTC, Oleg wrote:
> Because dynamic arrays are passed by value to functions. Will 
> it make another copy of array, if I'll pass array by value?

It is important to think of the underlying representation with a 
pointer and length passed by value. Since it is a pointer, the 
contents are never copied with a D slice, but reallocation or 
length changes won't be seen unless it is ref.

In general, I'm skeptical of ref arguments btw because they 
aren't needed as often as they are used. ref arrays, ref 
pointers, and ref class objects should only be used if you are 
going to reassign it to something new (or change the length of 
the array) and want that seen by the called - the contents are 
automatically passed by reference.

ref structs are sometimes useful, but even there I tend to avoid 
them. Value passing is often faster anyway (if the struct.sizeof 
<= pointer.sizeof it is a win every time, and even if bigger than 
that it often is) and generally simpler to follow.

So my advice is to break the habit of using ref everywhere in D, 
you usually don't need it.


More information about the Digitalmars-d-learn mailing list