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?

Reply via email to