如何使TR1 ::数组分配对齐的内存? [英] How to make tr1::array allocate aligned memory?

查看:207
本文介绍了如何使TR1 ::数组分配对齐的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以分配其器分配定义自己的分配器对准堆内存的一个std ::向量。
您可以使用分配对齐declspec在堆栈上C风格的数组。
但是你可以声明TR1 ::阵列,保证索引零元素会被排列?

You can allocate a std::vector which allocates aligned heap memory by defining your own allocator. You can allocate a c-style array on the stack using declspec align. But can you declare a tr1::array which guarantees that the element at index zero will be aligned?

推荐答案

TR1 ::阵列(和的std ::阵列的boost ::阵列)的POD,因此受到的内容占用的内存与阵列。因此,分配阵列但是你需要,并与安置构造它

tr1::array (and std::array and boost::array) are POD, so the memory occupied by the contents is coincident with the memory of the array. So, allocate the array however you need to, and construct it with placement new.

typedef std::tr1::array< MyClass, ary_sz > AryT;
void *array_storage = aligned_allocation( sizeof( AryT ) );
AryT *ary = new( array_storage ) AryT( initial_value );

这篇关于如何使TR1 ::数组分配对齐的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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