Question about dynamic arrays and slices

Steven Schveighoffer schveiguy at yahoo.com
Thu Jan 30 05:12:21 PST 2014


On Thu, 30 Jan 2014 05:43:55 -0500, Ary Borenszweig <ary at esperanto.org.ar>  
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?

Yes, it is intrinsic on how slices and arrays work.

> How can I safely pass around a dynamic array without being afraid  
> someone will keep an old copy of the data?

You can't. Why is that important? The use case (or case that you are  
troubled by) would help to explain I think.

-Steve


More information about the Digitalmars-d-learn mailing list