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?
That's how it is.
How can I safely pass around a dynamic array without being
afraid someone will keep an old copy of the data?
I don't understand your exact requirements. Do you want to make
sure that everyone sees the updates to the array, even after a
relocation?