[Issue 14467] arr.capacity sometimes erroneously returns 0

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun Apr 19 20:48:20 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=14467

--- Comment #1 from Ketmar Dark <ketmar at ketmar.no-ip.org> ---
it looks right to me. consider this code:

void main () {
  auto a0 = new int[10];
  auto a1 = a0[5..$];
  a1 ~= 42;
  a0 ~= 667;
}

here if `a1.capacity` will not be 0, the last item of `a1` will become `667`,
as druntime will reuse memory for `a0`, and that memory is already used by
`a1`.

as there is no dataflow analysis, compiler can't tell if `arr` in your case is
the only ref to the array. so compiler conservatively sets slice capacity to
`0`, triggering copy-on-append behavior.

--


More information about the Digitalmars-d-bugs mailing list