std::array alignment

前端 未结 4 1558
情歌与酒
情歌与酒 2020-12-11 03:08

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         


        
4条回答
  •  悲哀的现实
    2020-12-11 03:39

    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.

提交回复
热议问题