Proper way of passing array parameters to D functions -


1st question:

are d array function parameters passed reference, or value? also, language implements copy on write arrays? e.g.:

void foo(int[] arr) {     // arr local copy or ref external array?      arr[0] = 42; // how now? } 

2nd question:

suppose have large array passed function foo read-only parameter , should avoided as possible copying array, since assumed large object. following (or none of them) best declaration function foo:

void foo(const int[] bigarray)  void foo(in int[] bigarray)  void foo(const ref int[] bigarray) 

  1. technically, dynamic array int[] pointer , length. pointer , length copied onto stack, not array contents. arr[0] = 42; modify original array.
    on other side, static array int[30] plain old data type consisting of 30 consecutive ints in memory. so, function void foo(int[30] arr) copy 120 bytes onto stack start. in such case, arr[0] = 42; modifies local copy of array.

  2. according above, each of ways listed avoids copying array contents. so, whether need parameter const, in, const ref or otherwise depends on trying achieve besides avoiding array copy. example, if pass ref int [] arr parameter, not can modify contents, able modify pointer , length (for example, create wholly new array , assign arr visible outside function).

for further information, please refer corresponding articles on dlang site covering arrays , array slices.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -