@@ -579,7 +579,7 @@ a.length; // runtime value
579
579
580
580
p.dup; // error, length not known
581
581
s.dup; // creates an array of 3 elements, copies
582
- // elements s into it
582
+ // elements of s into it
583
583
a.dup; // creates an array of a.length elements, copies
584
584
// elements of a into it
585
585
---------
@@ -604,16 +604,22 @@ array.length = 7;
604
604
array = array[0..7];
605
605
---------
606
606
607
- $(P If the new array length is longer, the remainder is filled out with the
607
+ $(P If the new array length is longer, the array is reallocated if necessary,
608
+ preserving the existing elements. The new elements are filled out with the
608
609
default initializer.
609
610
)
610
611
612
+ $(H4 $(LNAME2 growing, Growing an Array))
613
+
611
614
$(P To maximize efficiency, the runtime always tries to resize the array
612
- in place to avoid extra copying. It will always do a copy if the new
613
- size is larger and the array was not allocated via the new operator or
614
- resizing in place would overwrite valid data in the array. For example:)
615
+ in place to avoid extra copying. It will do a copy if the new size
616
+ is larger and either:)
615
617
616
- $(SPEC_RUNNABLE_EXAMPLE_COMPILE
618
+ * The array was not $(DDSUBLINK spec/garbage, op_involving_gc, allocated by the GC).
619
+ * There is no spare $(RELATIVE_LINK2 capacity-reserve, capacity) in the array.
620
+ * Resizing in place would overwrite valid data still accessible in another slice.
621
+
622
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
617
623
---------
618
624
char[] a = new char[20];
619
625
char[] b = a[0..10];
@@ -623,10 +629,13 @@ char[] d = a;
623
629
b.length = 15; // always reallocates because extending in place would
624
630
// overwrite other data in a.
625
631
b[11] = 'x'; // a[11] and c[1] are not affected
632
+ assert(a[11] == char.init);
626
633
627
634
d.length = 1;
628
- d.length = 20; // also reallocates, because doing this will overwrite a and
629
- // c
635
+ assert(d.ptr == a.ptr); // unchanged
636
+
637
+ d.length = 20; // also reallocates, because doing this will overwrite a and c
638
+ assert(d.ptr != a.ptr);
630
639
631
640
c.length = 12; // may reallocate in place if space allows, because nothing
632
641
// was allocated after c.
@@ -642,14 +651,13 @@ a[15] = 'z'; // does not affect c, because either a or c has reallocated.
642
651
)
643
652
644
653
645
- $(P To guarantee copying behavior, use the .dup property to ensure
646
- a unique array that can be resized. Also, one may use the
647
- $(D .capacity) property to determine how many elements can be appended
648
- to the array without reallocating.
654
+ $(P To guarantee copying behavior, use the `.dup` property to ensure
655
+ a unique array that can be resized.
649
656
)
650
657
651
- $(P These issues also apply to appending arrays with the ~= operator.
652
- Concatenation using the ~ operator is not affected since it always
658
+ $(NOTE These issues also apply to
659
+ $(RELATIVE_LINK2 array-appending, appending arrays) with the `~=` operator.
660
+ Concatenation using the `~` operator is not affected since it always
653
661
reallocates.
654
662
)
655
663
@@ -701,16 +709,24 @@ array.length = i;
701
709
input from the console - it's unlikely to be longer than 80.
702
710
)
703
711
712
+ $(H3 $(LNAME2 capacity-reserve, `capacity` and `reserve`))
713
+
714
+ $(P The $(D capacity) property gives the maximum length the array
715
+ can grow to without reallocating. The spare capacity for array *a*
716
+ is `a.capacity - a.length`.)
704
717
$(P The $(D reserve)
705
718
function expands an array's capacity for use by the append operator.)
706
719
707
720
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
708
721
---------
709
722
int[] array;
710
723
size_t cap = array.reserve(10); // request
711
- array ~= [1, 2, 3, 4, 5];
712
724
assert(cap >= 10); // allocated may be more than request
725
+
726
+ int[] copy = array;
727
+ array ~= [1, 2, 3, 4, 5]; // grow in place
713
728
assert(cap == array.capacity);
729
+ assert(copy.ptr == array.ptr);
714
730
---------
715
731
)
716
732
0 commit comments