1

I understand that .reserve() reserves memory for the vector without actually modifying its size. But how is this implemented? How can you just reserve memory without allocating it?

EDIT: I'm asking specifically about how to reserve memory without allocating it, not about how std::vector works in general

NutCracker
  • 11,485
  • 4
  • 44
  • 68
H-005
  • 467
  • 3
  • 15
  • 1
    Does this answer your question? [How does c++ std::vector work?](https://stackoverflow.com/questions/3167272/how-does-c-stdvector-work) – OrangeDog Feb 13 '21 at 15:29
  • @OrangeDog I'm asking specifically about how to reserve memory, since I know how dynamic memory allocation works. Thanks for the suggestion though – H-005 Feb 13 '21 at 15:36
  • @OrangeDog Actually, I found this response (https://stackoverflow.com/a/3167551/13647419) to the question you linked, which touches a bit on the subject of reserving memory, but it still doesn't make too much sense. Is that memory used, or not? – H-005 Feb 13 '21 at 15:41
  • It does describe how `reserve` works. It allocates memory. – OrangeDog Feb 13 '21 at 16:13
  • Yeah, I was explained that `reserve` actually allocates memory, so I'll take a closer look at the answers that you sent as well, since I misunderstood – H-005 Feb 13 '21 at 16:20

3 Answers3

7

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. reserve allocates memory and changes the capacity, but not the size.

At any given time the following is true 0 <= size <= capacity. The capacity reflects the amount of memory allocated, the size reflects the number of constructed elements in that memory.

john
  • 85,011
  • 4
  • 57
  • 81
  • Oh... But from what I understand, the memory that is reserved can still be used in a different way, right? – H-005 Feb 13 '21 at 15:49
  • 2
    @H-005 No, that's not true (at least not true of vector). – john Feb 13 '21 at 15:49
  • @H-005 New elements can be constructed in the reserved memory but the vector still "owns" all the memory. There's no way to reuse or borrow the extra capacity. – Blastfurnace Feb 13 '21 at 15:52
2

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.

NutCracker
  • 11,485
  • 4
  • 44
  • 68
1

The size of a vector is the number of elements that it holds. The capacity of a vector is the number of elements that it could hold without allocating additional memory. reserve can increase the capacity by reallocating and copying the elements. That increases the capacity but doesn’t change the size.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165