如何使 tr1::array 分配对齐内存?

发布于 2024-09-16 02:02:24 字数 118 浏览 4 评论 0原文

您可以分配一个 std::vector,它通过定义自己的分配器来分配对齐的堆内存。 您可以使用 declspecalign 在堆栈上分配 C 样式数组。 但是你能声明一个 tr1::array 来保证索引零处的元素对齐吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芯好空 2024-09-23 02:02:24

tr1::array(以及std::arrayboost::array)都是POD,所以内容占用的内存与数组的内存。因此,请根据需要分配数组,并使用放置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::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 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文