www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15481] New: Reducing array.length triggers reallocation

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

          Issue ID: 15481
           Summary: Reducing array.length triggers reallocation
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: valentin.milea gmail.com

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".

This is contradicted by:

void main() {
    int[] arr;
    arr.length = 7;
    arr.length = 6; // not ok -- allocation reported
    arr = 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

--
Dec 27 2015