Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the data in nested std::arrays guaranteed to be contiguous?

Is the data in std::array<std::array<T,N>, M> guaranteed to be contiguous? For example:

#include <array>
#include <cassert>

int main()
{
    enum {M=4, N=7};
    typedef std::array<char,N> Row;
    typedef std::array<Row, M> Matrix;
    Matrix a;
    a[1][0] = 42;
    const char* data = a[0].data();

    /* 8th element of 1D data array should be the same as
       1st element of second row. */
    assert(data[7] == 42);
}

Is the assert guaranteed to succeed? Or, to put it another way, can I rely on there being no padding at the end of a Row?

EDIT: Just to be clear, for this example, I want the data of the entire matrix to be contiguous.

like image 472
Emile Cormier Avatar asked Mar 18 '12 21:03

Emile Cormier


People also ask

Is std :: array contiguous?

Yes the memory of std::array is contiguous.

Are arrays contiguous blocks of data?

A contiguous array is just an array stored in an unbroken block of memory: to access the next value in the array, we just move to the next memory address. This means arr is a C contiguous array because the rows are stored as contiguous blocks of memory.

Are vectors guaranteed to be contiguous?

Yes, the elements of a std::vector are guaranteed to be contiguous.

Are 2d arrays contiguous in C++?

The only way to create a contiguous "2d" array is as an array of arrays. @dasblinkenlight Its what I would use as well, but alas it doesn't meet the requirements of the question and return a memory backdrop of the structure as contiguous.


2 Answers

No, contiguity is not guaranteed in this case.

std::array is guaranteed to be an aggregate, and is specified in such a way that the underlying array used for storage must be the first data member of the type.

However, there is no requirement that sizeof(array<T, N>) == sizeof(T) * N, nor is there any requirement that there are no unnamed padding bytes at the end of the object or that std::array has no data members other than the underlying array storage. (Though, an implementation that included additional data members would be, at best, unusual.)

like image 51
James McNellis Avatar answered Oct 18 '22 14:10

James McNellis


They are very likely contiguous. If they are not, the compiler is actively fighting you there. There's no guarantee it won't insert padding but there's hardly a reason for it.

Is the assert guaranteed to succeed?

data[7] is an out-of-bounds access (undefined behaviour). The inner array object has only seven elements, so index 7 is not valid.

like image 43
R. Martinho Fernandes Avatar answered Oct 18 '22 13:10

R. Martinho Fernandes