View source code
Display the source code in object.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 object.reserve
Reserves capacity for a slice. The capacity is the size that the slice can grow to before the underlying array must be reallocated or extended.
size_t reserve(T)
(
ref T[] arr,
size_t newcapacity
) pure nothrow @trusted;
Returns
The new capacity of the array (which may be larger than the requested capacity).
Example
//Static array slice: no capacity. Reserve relocates.
int[4] sarray = [1, 2, 3, 4];
int[] slice = sarray[];
auto u = slice .reserve(8);
assert(u >= 8);
assert(&sarray[0] !is &slice[0]);
writeln(slice .capacity); // u
//Dynamic array slices
int[] a = [1, 2, 3, 4];
a .reserve(8); //prepare a for appending 4 more items
auto p = &a[0];
u = a .capacity;
a ~= [5, 6, 7, 8];
assert(p == &a[0]); //a should not have been reallocated
assert(u == a .capacity); //a should not have been extended
Authors
Walter Bright, Sean Kelly
License
Copyright © 1999-2022 by the D Language Foundation | Page generated by ddox.