Why does reverse also flips my other dynamic array?

Marco Leise via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 12 14:29:37 PDT 2015


Am Sat, 12 Sep 2015 10:55:50 +0000
schrieb "Namal" <sotis22 at mail.ru>:

> > Why is also b flipped here? This doesn't happen if I use static 
> > arrays.
> 
> nvm. I need to .dup that.

Correct, static arrays are value types and copied on
assignment. Dynamic arrays on the other hand are generally
slices of memory on the garbage collected heap represented by
just a pointer to the first element and a length. When you
assign those you only get a second reference to the same data.

Note that often the original dynamic array has additional
capacity beyond its length. This can be used to ~= additional
items without causing a reallocation, but is lost when you
do the assignment "b = a". (This is so you can't accidentally
append different items to both a and b and have them overwrite
each other.) You can query the actual capacity with a.capacity.

Just felt like writing down some knowledge you might need
along the way. :)

-- 
Marco



More information about the Digitalmars-d-learn mailing list