gpt4 book ai didi

c++ - 与 STL vector 对齐分配

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:19:08 25 4
gpt4 key购买 nike

我希望我的 vector 存储对齐(比如 16 字节边界),所以我将这个分配器作为 memalign() 的包装器,我想知道这是否是正确的方法分配用于 STL vector 的对齐内存。

template <class T , int Alignment=16>
class AlignedAllocator
{
public:
...

pointer allocate (size_type size, const_pointer *hint = 0) {
return (pointer) memalign(Alignment, size*sizeof (T));
};

void deallocate (pointer p, size_type size) {
free(p);
};
...
}

如果没有,是否有适用于 STL 容器的对齐分配器的可用实现?

PS: 我正在用 gcc 编译。

最佳答案

函数 memalign(3) 已过时,请尝试使用 posix_memalign(3)反而。除此之外,假设代码中的 ... 意味着包含其余所需的分配器元素,您的代码看起来不错。

关于c++ - 与 STL vector 对齐分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11600943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com