Question about dynamic arrays and slices

John Colvin john.loughran.colvin at gmail.com
Thu Jan 30 04:51:27 PST 2014


On Thursday, 30 January 2014 at 12:50:34 UTC, John Colvin wrote:
> On Thursday, 30 January 2014 at 10:43:55 UTC, Ary Borenszweig 
> wrote:
>> Hi,
>>
>> I just read this nice article about slices: 
>> http://dlang.org/d-array-article.html
>>
>> So I tried this code to see if I understood it correctly:
>>
>> ---
>> import std.stdio;
>>
>> void main() {
>>  auto a = new int[5];
>>  auto b = a;
>>
>>  a[0] = 1;
>>
>>  for(auto i = 0; i < 100; i++) {
>>    a ~= 0;
>>  }
>>
>>  a[0] = 2;
>>
>>  writefln("a[0] = %d", a[0]);
>>  writefln("b[0] = %d", b[0]);
>> }
>> ---
>>
>> This prints:
>>
>> a[0] = 2
>> b[0] = 1
>>
>> That is, "a" was resized to a point where it needed to 
>> reallocate its contents. "b" still holds a reference to the 
>> old data. When, after the for loop, I change a's data, b's 
>> data doesn't change.
>>
>> Is this expected behaviour?
>>
>> How can I safely pass around a dynamic array without being 
>> afraid someone will keep an old copy of the data?
>
> pass the array by ref, or make a wrapper struct that holds a 
> pointer to a slice, or a wrapper class that holds the slice.
>
> Alternatively, make a wrapper struct that disallows the append 
> operator and always use std.array.refAppender

woops, sorry that last one doesn't help.


More information about the Digitalmars-d-learn mailing list