加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 使用STL向量对齐分配

发布时间:2020-12-16 07:00:47 所属栏目:百科 来源:网络整理
导读:我希望我的向量存储对齐(比如16字节边界),所以我把这个分配器作为memalign()的包装器,我想知道这是否是分配与STL向量一起使用的对齐内存的正确方法. template class T,int Alignment=16class AlignedAllocator{public:... pointer allocate (size_type size,
我希望我的向量存储对齐(比如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).除此之外,假设代码中的…意味着包含剩余的所需分配器元素,您的代码看起来很好.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读