Making std::vector allocate aligned memory(使 std::vector 分配对齐的内存)
问题描述
是否可以使自定义结构的 std::vector
分配对齐的内存以使用 SIMD 指令进行进一步处理?如果可以使用 Allocator
,有没有人碰巧有这样一个他可以共享的分配器?
Is it possible to make std::vector
of custom structs allocate aligned memory for further processing with SIMD instructions? If it is possible to do with Allocator
, does anyone happen to have such an allocator he could share?
推荐答案
从 C++17 开始,只需使用 std::vector<__m256i>
或任何其他对齐类型.operator new
有对齐版本,它被 std::allocator
用于对齐类型(以及普通的 new
表达式,所以new __m256i[N]
从 C++17 开始也是安全的.
Starting in C++17, just use std::vector<__m256i>
or with any other aligned type. There's aligned version of operator new
, it is used by std::allocator
for aligned types (as well as by plain new
-expression, so new __m256i[N]
is also safe starting in C++17).
@MarcGlisse 发表了这样的评论,这是一个使其更显眼的答案.
There's a comment by @MarcGlisse saying this, making this an answer to make it more visible.
这篇关于使 std::vector 分配对齐的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使 std::vector 分配对齐的内存


基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 我有静态或动态 boost 库吗? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01