std::array 对齐 [英] std::array alignment

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

问题描述

在 Mac 上尝试 std::tr1::array 我得到 16 字节对齐.

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;

STL 中是否有任何行为类似于数组<T,N > 但保证不会自己填充,即

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  

推荐答案

该标准要求元素连续存储,这意味着如果 a 是一个数组,那么它遵守恒等式 &a[n] == &a[n] == &;a[0] + n 对于所有 0 <= 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)

据我所知,不能保证 sizeof(std::array) == sizeof(T)*N,但连续性语句断言这些值的存储就像在常规 C 数组中一样.如果您只有一个数组需要连续的值,则可以使用 sizeof(T)*std::array::size() 作为大小,使用 std::array::data() 作为数组的起始地址数组.

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::array 对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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