的std ::阵列对准 [英] std::array alignment

查看:150
本文介绍了的std ::阵列对准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试行的std :: TR1的::数组在Mac上我得到16字节对齐。

 的sizeof(INT)= 4;
的sizeof(性病:: TR1的::数组< INT,3>)= 16;
的sizeof(性病:: TR1的::数组< INT,4>)= 16;
的sizeof(性病:: TR1的::数组< INT,5个)= 32;
 

有没有在这行为就像阵&LT STL的东西; T,N>但保证不垫自行解决,即

 的sizeof(ARRAY< T,N>)= sizeof运算(T)* N
 

解决方案

的元素连续存储的标准要求,这意味着如果是一个数组,那么它服从的身份和放大器;一个[N] ==&放;一个[0] + n,用于所有0℃=正&其中; N (23.3.2.1 [array.overview]第1款)

据我所知,是没有保证的sizeof(STD ::阵列)==的sizeof(T)* N,但连续性声明称,该值存储就像在一个常规的C数组。 如果你只有一个需要是连续的值中的一个阵列,可以使用的sizeof(T)*的std ::数组::大小()的大小和std ::数组::数据()作为起始地址数组。

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  

解决方案

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.

这篇关于的std ::阵列对准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆