Bug 11561 – Built-in array .capacity returns 0 if array length is decreased
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-20T01:18:00Z
Last change time
2013-11-20T03:11:37Z
Assigned to
nobody
Creator
joseph.wakeling
Comments
Comment #0 by joseph.wakeling — 2013-11-20T01:18:38Z
Consider the following code:
int[] arr;
arr.length = 10;
writeln(arr.length, "\t", arr.capacity);
arr.length = 5;
writeln(arr.length, "\t", arr.capacity);
Expected output:
10 15
5 n
... where n >= 5.
Actual output:
10 15
5 0
Since clearly the capacity of the array of length 5 _isn't_ 0, there is something wrong in how the .capacity property is calculating its value after the array length is reduced.
Comment #1 by yebblies — 2013-11-20T02:58:19Z
No, this is correct. A capacity of zero simply means the slice cannot be appended to.
http://dlang.org/phobos/object.html#.capacity
If an append must reallocate a slice with no possibility of extension, then 0 is returned. This happens when the slice references a static array, or if another slice references elements past the end of the current slice.
Comment #2 by joseph.wakeling — 2013-11-20T03:11:37Z