You misunderstood one main thing: std::vector::reserve
actually allocates memory.
Let's say we create a custom Allocator
like:
template <typename T>
struct Allocator
{
using value_type = T;
Allocator() = default;
T* allocate( std::size_t N )
{
N *= sizeof( T );
std::cout << "Allocation " << N << " bytes" << std::endl;
return static_cast< T* >( ::operator new( N ) );
}
void deallocate( T *ptr, std::size_t N )
{
std::cout << "Deallocation " << (N * sizeof * ptr) << " bytes" << std::endl;
::operator delete( ptr );
}
};
If you use it like:
int main()
{
std::vector< int, Allocator< int > > v;
v.reserve( 100 );
}
The output would be:
Allocation 400 bytes
Deallocation 400 bytes
You can play with it here.