Slices: Termination of Sharing
Explore how slices share array elements in D programming and what triggers termination of this sharing. Understand how modifying slice length impacts memory and how the capacity property helps manage element additions efficiently. This lesson equips you with knowledge to handle slices and optimize array operations.
Slice sharing #
It is possible to access the same elements by more than one slice. For example, the first two of the eight elements below are being accessed through three slices:
The effect of the modification to the second element of quarter is seen through all slices.
Making a slice longer may terminate sharing #
When viewed this way, slices provide shared access to elements. This sharing poses the question of what happens when a new element is added to one of the slices. Since multiple slices can provide access to the same elements, there may not be room to add elements to a slice without stomping on the elements of other slices of the same array.
D disallows element stomping and answers this question by terminating the sharing relationship if there is no room for the new ...