Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the memory layout of vector of arrays?

can anybody explaine the memory layout of

std::vector<std::array<int, 5>> vec(2) 

does it provide contiguous memory block of a 2D array with 2 rows of 5 elements?

To my understanding, the vector of vectors

std::vector<std::vector<int>> vec(2, std::vector<int>(5)) 

provide the memory layout of two contiguous arrays of length 5 elements in different locations in memory.

Will it be the same for the vector of arrays?

like image 341
Const Avatar asked Jan 15 '19 10:01

Const


People also ask

How are vectors stored in memory?

Vectors are assigned memory in blocks of contiguous locations. When the memory allocated for the vector falls short of storing new elements, a new memory block is allocated to vector and all elements are copied from the old location to the new location. This reallocation of elements helps vectors to grow when required.

Do vectors use more memory than arrays?

Vectors are efficient and flexible. They do require a little more memory than arrays, but this tradeoff is almost always worth the benefits.

What type of array is a vector?

Vectors are known as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container automatically.

Does vector Reserve allocate memory?

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.


1 Answers

Arrays do not have any indirection, but just store their data "directly". That is, a std::array<int, 5> literally contains five ints in a row, flat. And, like vectors, they do not put padding between their elements, so they're "internally contiguous".

However, the std::array object itself may be larger than the set of its elements! It is permitted to have trailing "stuff" like padding. So, although likely, it is not necessarily true that your data will all be contiguous in the first case.

An int +----+ |    | +----+  A vector of 2 x int +----+----+----+-----+        +----+----+ | housekeeping | ptr |        | 1  |  2 | +----+----+----+-----+        +----+----+                    |          ^                    \-----------  An std::array<int, 5> +----+----+----+----+----+-----------> | 1  |  2 |  3 |  4 |  5 | possible cruft/padding.... +----+----+----+----+----+----------->  A vector of 2 x std::array<int, 5> +----+----+----+-----+        +----+----+----+----+----+----------------------------+----+----+----+----+----+-----------> | housekeeping | ptr |        | 1  |  2 |  3 |  4 |  5 | possible cruft/padding.... | 1  |  2 |  3 |  4 |  5 | possible cruft/padding.... +----+----+----+-----+        +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->                    |          ^                    \----------- 

And, even if it were, due to aliasing rules, whether you'd be able to use a single int* to navigate all 10 numbers would potentially be a different matter!

All in all, a vector of ten ints would be clearer, completely packed, and possibly safer to use.

In the case of a vector of vectors, a vector is really just a pointer plus some housekeeping, hence the indirection (as you say).

like image 107
Lightness Races in Orbit Avatar answered Oct 12 '22 15:10

Lightness Races in Orbit