www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Assoc. array as 'in' function parameter

reply Sark7 <sark7 mail333.com> writes:
[code]
void foo(int[int] foobar) {
   foobar[1] = 1;
}
void main() {
   int[int] bar;
   foo(bar);
   printf("%d", bar.length);
}
[/code]

If assoc. array 'bar' is not initialized, then:
If 'foobar' is 'in', program outputs '0', i.e. all changes in array are 
discarded.
If 'foobar' is 'out' or 'inout', program outputs '1'.

If 'bar' is initialized (it can be empty or not), with 'in', 'inout', 
'out' program output is '1'.
May 02 2004
parent "Walter" <newshound digitalmars.com> writes:
It's a similar issue as with dynamic arrays. Inserting elements into one
involves reallocating the storage for it, and since 'in' uses value
semantics, it's working on a copy, not the original.

"Sark7" <sark7 mail333.com> wrote in message
news:opr7dyu6bkut8jae news.digitalmars.com...
 [code]
 void foo(int[int] foobar) {
    foobar[1] = 1;
 }
 void main() {
    int[int] bar;
    foo(bar);
    printf("%d", bar.length);
 }
 [/code]

 If assoc. array 'bar' is not initialized, then:
 If 'foobar' is 'in', program outputs '0', i.e. all changes in array are
 discarded.
 If 'foobar' is 'out' or 'inout', program outputs '1'.

 If 'bar' is initialized (it can be empty or not), with 'in', 'inout',
 'out' program output is '1'.

May 24 2004