Search code examples
c++11moveheap-memorystdarraystack-memory

Is it possible to move an std::array into a std::vector?


This is a question about the interaction of stack memory and heap memory and the particular case of going from stack to heap via the std::array and std::vector classes.

In principle std::array<T> can be seen as a pointer to the first elements, plus some compile time information about the size of the array. Would it be possible to have std::vector<T> constructor that takes into account that fact and tries to move contents of the array into the vector just by copying the pointer.

A use case would be, that one has a function that returns a std::array<double, >

std::array<double, 20> fun(){...};

but one later decides to assign it to a std::vector without the necessity of copying element by element.

std::vector<double> v = fun(); // not working code

Right now one has to do

std::array<double, 20> tmp = fun();
std::vector<double> v(tmp.begin(), tmp.end());

Which actually does some redundant work which wouldn't be necessary if this were possible std::vector<double> v(std::move(tmp)); \\ not working code.

The memory layout of std::vector and std::array is the same, so that is not and obstacle.

I understand that the main obstacle could be that std::array elements are in the stack while std::vector elements are in the heap. It is clear that even if one writes the move constructor for std::vector still the memory from the stack will be irrevocably destructed.

So I guess that this question can also be read as:

Is there a way to move memory from the stack to the heap (whatever that means) and if that can be combined with a move constructor?

Or if std::vector can have in principle a move constructor from a std::array?

MWE:

#include<array>
#include<vector>

std::array<double, 20> fun(){return {};} // don't change this function

int main(){
    std::array<double, 20> arr = fun(); // ok
    std::vector<double> v(arr.begin(), arr.end()); // ok, but copies and the allocation is duplicated
    std::vector<double> v2 = fun(); // not working, but the idea is that the work is not duplicated
}

Solution

  • Is there a way to move memory from the stack to the heap (whatever that means) and if that can be combined with a move constructor?

    I personally love the "whatever that means" bit. Let's ponder on this for a while. Moving something from stack to heap would suddenly mean that that part of the stack suddenly becomes marked as a heap-allocated region and subject to regular destruction.

    The problem with that is that stack is contiguous and is destroyed by, well popping things off it. You can't just say "hey, leave this memory bit out" - any consecutive stack allocation and deallocation would need to jump "over" that part.

    To illustrate:

    |                      |
    |----------------------|
    | stack block 1        |
    |----------------------|
    | your vector          |
    |----------------------|
    | stack block 2        |
    |----------------------|
    |-                    -|
    

    If you wanted to unwind those two blocks, you'd need to first decrease the stack pointer by the size of the block 2 pointer, then by the size of your vector and the block 1. This really isn't something that can happen.

    Hence, the only feasible solution here is to make a copy into the heap memory region. However, those copies are much faster than a lot of people expect. Even if the vector has a few megabytes, the memory controller can just swap around some pages, I suppose, and not have to physically send electrical signals corresponding to the bits of the data.

    Besides, any resize of the vector would need to cause a reallocation either way. Since the array occupies precisely as much memory as it needs, even adding a single element would trigger the copy you're trying to avoid so much.