Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

core.internal.array.capacity

This module contains support for controlling dynamic arrays' capacity and length
License:
Distributed under the Boost Software License 1.0. (See accompanying file LICENSE)
@trusted void _d_arrayshrinkfit(Tarr : T[], T)(Tarr arr, bool isshared);
Shrink the "allocated" length of an array to be the exact size of the array.
It doesn't matter what the current allocated length of the array is, the user is telling the runtime that he knows what he is doing.
Parameters:
T the type of the elements in the array (this should be unqualified)
Tarr arr array to shrink. Its .length is element length, not byte length, despite void type
bool isshared true if the underlying data is shared
pure nothrow @trusted size_t _d_arraysetcapacityPureNothrow(T)(size_t newcapacity, void[]* p, bool isshared);
Set the array capacity.
If the array capacity isn't currently large enough to hold the requested capacity (in number of elements), then the array is resized/reallocated to the appropriate size.
Pass in a requested capacity of 0 to get the current capacity.
Parameters:
T the type of the elements in the array (this should be unqualified)
size_t newcapacity requested new capacity
void[]* p pointer to array to set. Its length is left unchanged.
bool isshared true if the underlying data is shared
Returns:
the number of elements that can actually be stored once the resizing is done
@trusted size_t _d_arraysetlengthT(Tarr : T[], T)(return ref scope Tarr arr, size_t newlength);
Resize a dynamic array by setting its .length property.
Newly created elements are initialized based on their default value. If the array's elements initialize to 0, memory is zeroed out. Otherwise, elements are explicitly initialized.
This function handles memory allocation, expansion, and initialization while maintaining array integrity.
void main()
{
    int[] a = [1, 2];
    a.length = 3; // Gets lowered to `_d_arraysetlengthT!(int)(a, 3, false)`
}
Parameters:
Tarr arr The array to resize.
size_t newlength The new value for the array's .length.
Returns:
The resized array with updated length and properly initialized elements.
Throws:
OutOfMemoryError if allocation fails.