我希望我的 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/