Array as an argument, ambiguous behaviour.

Cooler kulkin at hotbox.ru
Thu Jan 30 07:59:47 PST 2014


On Thursday, 30 January 2014 at 15:51:44 UTC, Tobias Pankrath 
wrote:
> On Thursday, 30 January 2014 at 15:49:35 UTC, Cooler wrote:
>>>> I agree. I just want that the case can be expressed in 
>>>> language syntax more obvious - something like "fun(int[] 
>>>> const x){}" to emphasize that I understand that fun() can 
>>>> change content of array, and cannot change the 
>>>> {pointer,size} pair.
>>>
>>> That's what fun(int[] x) does :)
>>>
>>> -Steve
>>
>> Again...
>> void fun(int[] x){ x ~= 5; }
>> auto a = new int[10];
>> fun(a); // Can you predict the content of 'a'?
>
>
> It's [0, 0, 0, 0, 0, 0, 0, 0, 0, 0].

No!!! It depends how runtime allocates memory for the array. Read 
http://dlang.org/d-array-article.html.
If 'a' has internal space enough to place '5' the caller will see
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5].


More information about the Digitalmars-d-learn mailing list