Reducing array.length triggers reallocation

milentin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 27 02:09:01 PST 2015


I've just started learning D and noticed a bug, but wanted to 
confirm it here before reporting it.

According to spec: "If the new array length is shorter, the array 
is not reallocated, and no data is copied. It is equivalent to 
slicing the array". Contradicted by a trivial program:

void main() {
     int[] arr;
     arr.length = 7;
     arr.length = 6; // not ok -- allocation
     int[] slice = arr[0..5]; // ok -- no allocation
}

-------------------------------------------------------
dmd -profile=gc test.d
(DMD32 D Compiler v2.069.2)
-------------------------------------------------------
bytes allocated, allocations, type, function, file:line
              28               1 int[] D main test.d:3
              24               1 int[] D main test.d:4


More information about the Digitalmars-d-learn mailing list