<div>Hello List</div><div><br></div><div>As per TDPL and D documentation, shrinking a dynamic array should not relocate it. But that is exactly what is happening.</div><div>On shrinking an array, its capacity is getting reduced to 0. Surprisingly the capacity after a shrink is even less than the length</div>
<div>of the array. As a result the array gets relocated as soon as another element is added to it.</div><div><br></div><div>I have to call reserve again after shrinking it to make sure that the array is not relocated.</div>
<div><br></div><div>Is this the expected behavior?</div><div><br></div><div>Regards</div><div>- Puneet</div><div><br></div><div>P.S. Please compile the following code to see yourself.</div><div><br></div><div>import std.stdio;</div>
<div><br></div><div>void main() {</div><div> uint [] arr;</div><div> reserve(arr, 2048);</div><div> writeln("After reservation:");</div><div> writefln("arr is at: %10s, length: %6s, capacity %6s",</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> arr.ptr, arr.length, capacity(arr));</div><div> // Grow the array</div><div> for (size_t i = 0; i < 64; ++i) arr ~= i;</div><div><br></div><div> writeln("After appending:");</div>
<div> writefln("arr is at: %10s, length: %6s, capacity %6s",</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> arr.ptr, arr.length, capacity(arr));</div><div><br></div><div> arr.length = 32;</div>
<div> // reserve(arr, 2048); // does not relocate if uncommented</div><div><br></div><div> writeln("After setting length:");</div><div> writefln("arr is at: %10s, length: %6s, capacity %6s",</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span> arr.ptr, arr.length, capacity(arr));</div><div><br></div><div> // grow the array again</div><div> for (size_t i = 0; i < 32; ++i) arr ~= i;</div><div> writeln("After appending again:");</div>
<div> writefln("arr is at: %10s, length: %6s, capacity %6s",</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> arr.ptr, arr.length, capacity(arr));</div><div>}</div>