Normally I call reserve
on a std::vector
immediately after constructing it. Wouldn't this typically cause the std::vector
's existing heap allocation to be destroyed and replaced with a new one? Is there a way to reserve the memory at construction time rather than allocate heap space and then immediately destroy it? Or is there an implemenatation trick within the std::vector
to ensure this is not an issue?
The available constructors only seem to be able to be useful for filling the std::vector
with values, rather than reserving space explicitly.
An std::vector manages its own memory. You can use the reserve() and resize() methods to have it allocate enough memory to fit a given amount of items: std::vector<int> vec1; vec1. reserve(30); // Allocate space for 30 items, but vec1 is still empty.
C++ Vector Library - reserve() Function The C++ function std::vector::reserve() requests to reserve vector capacity be at least enough to contain n elements. Reallocation happens if there is need of more space.
vector::reserve does allocate memory, so your question about reserving memory without allocating is incorrect. The point is that reserving memory can be done without changing the vectors size. Basically a vector has two sizes, it's size and it's capacity.
Vectors are assigned memory in blocks of contiguous locations. When the memory allocated for the vector falls short of storing new elements, a new memory block is allocated to vector and all elements are copied from the old location to the new location. This reallocation of elements helps vectors to grow when required.
Your question is based on a false premise, namely that a default-constructed std::vector<T>
will perform a [zero-length] allocation.
There is literally no reason for it to do so. A fresh vector should have capacity zero (though this is required by sanity, not by the standard).
As such, your goal is already inherently satisfied.
To be blunt, the standard library is not quite that stupid.
The reason may be ironic, we are running out of function signature.
The requirement comes from the using scenario that we exactly know how many elements we will save into vector but we really really don't like the n-duplicated elements constructor:
std::vector( size_type count, const T& value = T())
Unfortunately the signature is occupied by the above constructor. Any other possible signature may cause issue. E.g.
vector(size_type count, size_type reserve_count)
will conflicts with above n-duplicated elements constructor for vector of T(size_type)
.
vector(size_type count, const T& value = T(), size_type reserve_count)
is a possible solution but it is too long and still boring. We need to construct a default value we never use while calling auto v = vector<T>(0, T(), reserve_count)
Other feasible solutions:
Provides a function like make_pair/make_unique.
Defines a Reserver which derived from Allocator, so we can use the constructor vector( const Allocator& alloc ) like
auto v = vector<Type>(new Reserver(reserve_count));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With