Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
January 12, 2015 Set null as function array parameter | ||||
---|---|---|---|---|
| ||||
Hello. How can I call a function with null as parameter, which I don't want to set. For example: void foo(ref int[] param1) {} I can't call this function like: foo(null); Is it possible to set default value for an array parameter or pass null/empty array? I can create empty array and pass it, but it looks ugly: int[] x; foo(x); Thanks. |
January 12, 2015 Re: Set null as function array parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Oleg | On Monday, 12 January 2015 at 15:51:17 UTC, Oleg wrote:
> void foo(ref int[] param1) {}
Why are you using ref? Take that off and you can pass any array, including null, with ease.
The only difference is changes to length won't be seen outside the foo function.
|
January 12, 2015 Re: Set null as function array parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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. |
January 12, 2015 Re: Set null as function array parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Oleg Attachments: | On Mon, 12 Jan 2015 16:32:30 +0000 Oleg via Digitalmars-d-learn <digitalmars-d-learn@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. |
January 12, 2015 Re: Set null as function array parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Monday, 12 January 2015 at 16:44:42 UTC, ketmar via Digitalmars-d-learn wrote:
> 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.
Thank you.
|
January 12, 2015 Re: Set null as function array parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Oleg Attachments: | On Mon, 12 Jan 2015 16:53:59 +0000 Oleg via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > On Monday, 12 January 2015 at 16:44:42 UTC, ketmar via Digitalmars-d-learn wrote: > > 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. > > Thank you. be careful with chaning dynamic array length though. it won't be seen outside of function, as Adam wrote. and it can cause some side-effects like partially modifyed array (if you modified some array elements and then changes length, which causes reallocing). |
January 12, 2015 Re: Set null as function array parameter | ||||
---|---|---|---|---|
| ||||
Posted in reply to Oleg | 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.
|
Copyright © 1999-2021 by the D Language Foundation