Skip to content

Commit 29beca9

Browse files
committed
Define capacity & improve example
1 parent 1b8e119 commit 29beca9

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

spec/arrays.dd

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -754,18 +754,22 @@ array.length = i;
754754

755755
$(H3 $(LNAME2 capacity-reserve, `capacity` and `reserve`))
756756

757-
$(P The $(D capacity) property determines how many elements can be appended
758-
to the array without reallocating.)
757+
$(P The $(D capacity) property gives the maximum length the array
758+
can grow to without reallocating. The spare capacity for array *a*
759+
is `a.capacity - a.length`.)
759760
$(P The $(D reserve)
760761
function expands an array's capacity for use by the append operator.)
761762

762763
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
763764
---------
764765
int[] array;
765766
size_t cap = array.reserve(10); // request
766-
array ~= [1, 2, 3, 4, 5];
767767
assert(cap >= 10); // allocated may be more than request
768+
769+
int[] copy = array;
770+
array ~= [1, 2, 3, 4, 5]; // grow in place
768771
assert(cap == array.capacity);
772+
assert(copy.ptr == array.ptr);
769773
---------
770774
)
771775

0 commit comments

Comments
 (0)