简体   繁体   中英

How are arrays of type std::vector<std::array<T, N>> or std::array<std::vector<T>,N> stored in memory?

I know that std::vector<T> allocates dynamic memory on the heap. I also know that std::array<T,N> allocates memory on the stack.

But how is memory allocated when I merge both containers together?

Like fe:

std::vector<std::array<T, N>> a;

or

std::array<std::vector<T>,N> a;

By:

std::vector<std::array<T, N>> a;
  • Is the resuming object sequence/array of a fully stored on the heap or are parts of it shared between the heap and the stack?

By:

std::array<std::vector<T>,N> a;
  • Is the resuming object sequence/array of a fully stored on the stack or are parts of it shared between the stack and the heap?

Thank you very much for participating.

Basically, std::array<T, N> stores the T objects within the object itself as if they were ordinary data members, whereas std::vector<T> allocates a buffer on the heap and constructs the T objects on that memory .

When it comes to std::array<T, N> , since the T objects are inside the std::array itself, whether these T objects are allocated on the heap or the stack depends on where the std::array<T, N> is allocated :

  • If the std::array<T, N> is allocated on the stack, so will be the T objects.

  • If the std::array<T, N> is allocated on the heap (eg, new std::array<T, N> ), so will be the T objects.


std::vector<std::array<T, N>>

The vector stores all the std::array<T, N> objects in its internal buffer, which is allocated on the heap. That is, assuming vec_of_arrs has automatic storage duration :

std::vector<std::array<T, N>> vec_of_arrs;

Only the object vec_of_arrs is allocated on the stack. Its internal buffer – where a contiguous sequence of std::array<T, N> objects are created – is allocated on the heap. Since the T objects are stored directly within the std::array , they are also constructed on that memory, ie, the heap.


std::array<std::vector<T>,N>

The std::array stores the N objects of type std::vector<T> directly as data members within itself. Therefore, the std::vector<T> objects will be on the stack if the std::array containing them is allocated on the stack. However, each vector's internal buffer is allocated on the heap, and so are the T objects since they are constructed on that buffer. That is, assuming arr_of_vecs has automatic storage duration :

std::array<std::vector<T>,N> arr_of_vecs;

The object arr_of_vecs is allocated on the stack. The std::vector<T> objects are allocated within the std::array object, so they are on the stack as well (ie, the std::array holds a contiguous sequence of std::vector<T> objects). However, the internal buffer of these std::vector<T> objects are allocated on the heap, and the T objects are constructed on that memory, ie, the heap.

Consider the following code:

struct S
{
    int _i;
    /* ... */
};

int main()
{
    S s1;
    S* s2 = new S{};
    return 0;
}

The instance s1 is on the stack, and so are all its members. The content pointed to by s2 is allocated on the heap, and so are all its members .

Now, your examples:

// all the instances of std::array<T, N> are on the heap,
// since std::vector allocates on the heap
std::vector<std::array<T, N>>

// the array itself is on the stack, and also the vector instances,
// but the content of the vectors is on the heap, as std::vector allocates on the heap
std::array<std::vector<T>,N>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM