View source code
Display the source code in std/experimental/allocator/package.d from which this page was generated on github.
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 local clone.

Function std.experimental.allocator.shrinkArray

Shrinks an array by delta elements.

bool shrinkArray(T, Allocator) (
  auto ref Allocator alloc,
  ref T[] array,
  size_t delta
);

If array.length < delta, does nothing and returns false. Otherwise, destroys the last array.length - delta elements in the array and then reallocates the array's buffer. If reallocation fails, fills the array with default-initialized data.

Parameters

NameDescription
T element type of the array being created
alloc the allocator used for getting memory
array a reference to the array being shrunk
delta number of elements to remove (upon success the new length of array is array.length - delta)

Returns

true upon success, false if memory could not be reallocated. In the latter case, the slice array[$ - delta .. $] is left with default-initialized elements.

Throws

The first two overloads throw only if alloc's primitives do. The overloads that involve copy initialization deallocate memory and propagate the exception if the copy operation throws.

Example

int[] a = theAllocator.makeArray!int(100, 42);
writeln(a.length); // 100
assert(theAllocator.shrinkArray(a, 98));
writeln(a.length); // 2
writeln(a); // [42, 42]

Authors

Andrei Alexandrescu

License

Boost License 1.0.