Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::array alignment

Trying out std::tr1::array on a mac i'm getting 16 byte alignment.

sizeof(int) = 4;  
sizeof( std::tr1::array< int,3 > ) = 16;  
sizeof( std::tr1::array< int,4 > ) = 16;    
sizeof( std::tr1::array< int,5 > ) = 32;

Is there anything in the STL that behaves like array< T,N > but is guaranteed to NOT pad itself out, i.e.

sizeof( ARRAY< T, N> ) = sizeof(  T )*N  
like image 817
centaurian_slug Avatar asked Nov 24 '11 22:11

centaurian_slug


People also ask

Is std :: array contiguous?

Yes the memory of std::array is contiguous.

What is std :: Aligned_storage?

The type defined by std::aligned_storage<>::type can be used to create uninitialized memory blocks suitable to hold the objects of given type, optionally aligned stricter than their natural alignment requirement, for example on a cache or page boundary.

Is std :: array initialized?

std::array contains a built-in array, which can be initialized via an initializer list, which is what the inner set is.


1 Answers

The standard mandates that the elements "are stored contiguously, meaning that if a is an array, then it obeys the identity &a[n] == &a[0] + n for all 0 <= n < N." (23.3.2.1 [array.overview] paragraph 1)

As far as I know, there is no guarantee that sizeof(std::array) == sizeof(T)*N, but the contiguity statement asserts that the values are stored just like in a regular C array. If you only have one array of values that need to be contiguous, you can use sizeof(T)*std::array::size() as the size and std::array::data() as the starting address of the array.

like image 78
Ernavar Avatar answered Sep 16 '22 19:09

Ernavar