Set null as function array parameter

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 12 08:44:31 PST 2015


On Mon, 12 Jan 2015 16:32:30 +0000
Oleg via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com> wrote:

> On Monday, 12 January 2015 at 15:59:43 UTC, Adam D. Ruppe wrote:
> > Why are you using ref? Take that off and you can pass any 
> > array, including null, with ease.
> 
> Because dynamic arrays are passed by value to functions. Will it 
> make another copy of array, if I'll pass array by value?
> 
> Looks like it won't (I've checked pointers), but I read in wiki 
> (http://en.wikibooks.org/wiki/D_%28The_Programming_Language%29/d2/Strings_and_Dynamic_Arrays#Passing_Dynamic_Arrays_to_Functions) 
> that it will copy "structure that contains the -pointer to the 
> first element- and the length is copied and passed". Does it mean 
> something else? maybe i don't understand it correctly.
nope, it means exactly what is written there. except that dynamic array
is represented by struct like this:

  struct {
    void *dataptr;
    size_t itemCount;
  }

this is what D calls "dynamic array", and this is what passed by value:
struct with two members. dynamic array contents are *not* a part of
"dynamic array type".

yes, this is confusing.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150112/9eeb1c43/attachment.sig>


More information about the Digitalmars-d-learn mailing list