What does std::vector look like in memory?

前端 未结 6 685
耶瑟儿~
耶瑟儿~ 2021-02-01 13:10

I read that std::vector should be contiguous. My understanding is, that its elements should be stored together, not spread out across the memory. I have simply acce

相关标签:
6条回答
  • 2021-02-01 13:37

    In terms of the actual structure, an std::vector looks something like this in memory:

    struct vector {    // Simple C struct as example (T is the type supplied by the template)
      T *begin;        // vector::begin() probably returns this value
      T *end;          // vector::end() probably returns this value
      T *end_capacity; // First non-valid address
      // Allocator state might be stored here (most allocators are stateless)
    };
    

    Relevant code snippet from the libc++ implementation as used by LLVM

    Printing the raw memory contents of an std::vector:
    (Don't do this if you don't know what you're doing!)

    #include <iostream>
    #include <vector>
    
    struct vector {
        int *begin;
        int *end;
        int *end_capacity;
    };
    
    int main() {
        union vecunion {
            std::vector<int> stdvec;
            vector           myvec;
            ~vecunion() { /* do nothing */ }
        } vec = { std::vector<int>() };
        union veciterator {
            std::vector<int>::iterator stditer;
            int                       *myiter;
            ~veciterator() { /* do nothing */ }
        };
    
        vec.stdvec.push_back(1); // Add something so we don't have an empty vector
    
        std::cout
          << "vec.begin          = " << vec.myvec.begin << "\n"
          << "vec.end            = " << vec.myvec.end << "\n"
          << "vec.end_capacity   = " << vec.myvec.end_capacity << "\n"
          << "vec's size         = " << vec.myvec.end - vec.myvec.begin << "\n"
          << "vec's capacity     = " << vec.myvec.end_capacity - vec.myvec.begin << "\n"
          << "vector::begin()    = " << (veciterator { vec.stdvec.begin() }).myiter << "\n"
          << "vector::end()      = " << (veciterator { vec.stdvec.end()   }).myiter << "\n"
          << "vector::size()     = " << vec.stdvec.size() << "\n"
          << "vector::capacity() = " << vec.stdvec.capacity() << "\n"
          ;
    }
    
    0 讨论(0)
  • 2021-02-01 13:39

    std::vector being a contiguous container means exactly what you think it means.

    However, many operations on a vector can re-locate that entire piece of memory.

    One common case is when you add element to it, the vector must grow, it can re-allocate and copy all elements to another contiguous piece of memory.

    0 讨论(0)
  • 2021-02-01 13:40

    So what does it exactly mean, that std::vector is a contiguous container and why do its elements move? Does it maybe store them together, but moves them all together, when more space is needed?

    That's exactly how it works and why appending elements does indeed invalidate all iterators as well as memory locations when a reallocation takes place¹. This is not only valid since C++17, it has been the case ever since.

    There are a couple of benefits from this approach:

    • It is very cache-friendly and hence efficient.
    • The data() method can be used to pass the underlying raw memory to APIs that work with raw pointers.
    • The cost of allocating new memory upon push_back, reserve or resize boil down to constant time, as the geometric growth amortizes over time (each time push_back is called the capacity is doubled in libc++ and libstdc++, and approx. growths by a factor of 1.5 in MSVC).
    • It allows for the most restricted iterator category, i.e., random access iterators, because classical pointer arithmetic works out well when the data is contiguously stored.
    • Move construction of a vector instance from another one is very cheap.

    These implications can be considered the downside of such a memory layout:

    • All iterators and pointers to elements are invalidate upon modifications of the vector that imply a reallocation. This can lead to subtle bugs when e.g. erasing elements while iterating over the elements of a vector.
    • Operations like push_front (as std::list or std::deque provide) aren't provided (insert(vec.begin(), element) works, but is possibly expensive¹), as well as efficient merging/splicing of multiple vector instances.

    ¹ Thanks to @FrançoisAndrieux for pointing that out.

    0 讨论(0)
  • 2021-02-01 13:45

    It roughly looks like this (excuse my MS Paint masterpiece):

    The std::vector instance you have on the stack is a small object containing a pointer to a heap-allocated buffer, plus some extra variables to keep track of the size and and capacity of the vector.


    So it seems as though when I push_back() to the numbers vector, its older elements change their location.

    The heap-allocated buffer has a fixed capacity. When you reach the end of the buffer, a new buffer will be allocated somewhere else on the heap and all the previous elements will be moved into the new one. Their addresses will therefore change.


    Does it maybe store them together, but moves them all together, when more space is needed?

    Roughly, yes. Iterator and address stability of elements is guaranteed with std::vector only if no reallocation takes place.


    I am aware, that std::vector is a contiguous container only since C++17

    The memory layout of std::vector hasn't changed since its first appearance in the Standard. ContiguousContainer is just a "concept" that was added to differentiate contiguous containers from others at compile-time.

    0 讨论(0)
  • 2021-02-01 13:50

    If you try coding it this way, you will see that the values remain the same and address for each value in the vector has a difference of 4 with its adjacent element (interesting).

    std::vector<int> numbers;
    std::vector<int*> ptr_numbers;
    
    // adding values 0 up to 8 in the vector called numbers
    for (int i = 0; i < 8; i++) {
        numbers.push_back(i);
    
    }
    
    // printing out the values inside vector numbers 
    //and storing the address of each element of vector called numbers inside the ptr_numbers.
    for (int i = 0; i != numbers.size(); i++) {
        cout << numbers[i] << endl;
        ptr_numbers.push_back(&numbers[i]);
    }
    cout << "" << endl;
    
    // printing out the values of each element of vector ptr_numbers
    for (int y = 0; y != ptr_numbers.size(); y++) {
        cout << *ptr_numbers[y] << endl;
    }
    
    // printing out the address of each element of vector ptr_numbers
    for (int y = 0; y != ptr_numbers.size(); y++) {
        cout << &ptr_numbers[y] << endl;
    }
    

    When you loop through both vectors. They will output the same values.

    0 讨论(0)
  • 2021-02-01 13:54

    The Answer

    It's a single contiguous storage (a 1d array). Each time it runs out of capacity it gets reallocated and stored objects are moved to the new larger place — this is why you observe addresses of the stored objects changing.

    It has always been this way, not since C++17.

    TL; DR

    The storage is growing geometrically to ensure the requirement of the amortized O(1) push_back(). The growth factor is 2 (Capn+1 = Capn + Capn) in most implementations of the C++ Standard Library (GCC, Clang, STLPort) and 1.5 (Capn+1 = Capn + Capn / 2) in the MSVC variant.

    If you pre-allocate it with vector::reserve(N) and sufficiently large N, then addresses of the stored objects won't be changing when you add new ones.

    In most practical applications is usually worth pre-allocating it to at least 32 elements to skip the first few reallocations shortly following one other (0→1→2→4→8→16).

    It is also sometimes practical to slow it down, switch to the arithmetic growth policy (Capn+1 = Capn + Const), or stop entirely after some reasonably large size to ensure the application does not waste or grow out of memory.

    Lastly, in some practical applications, like column-based object storages, it may be worth giving up the idea of contiguous storage completely in favor of a segmented one (same as what std::deque does but with much larger chunks). This way the data may be stored reasonably well localized for both per-column and per-row queries (though this may need some help from the memory allocator as well).

    0 讨论(0)
提交回复
热议问题