我希望我的向量存储对齐(比如16字节边界),所以我把这个分配器作为memalign()的包装器,我想知道这是否是分配与STL向量一起使用的对齐内存的正确方法.
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).除此之外,假设代码中的…意味着包含剩余的所需分配器元素,您的代码看起来很好.