C++11内存对齐之std::aligned_storage与alignas与alignof

1. std::aligned_storage

插播一下POD的含义:Plain old data structure,缩写为POD,是C++语言的标准中定义的一类数据结构,POD适用于需要明确的数据底层操作的系统中。POD通常被用在系统的边界处,即指不同系统之间只能以底层数据的形式进行交互,系统的高层逻辑不能互相兼容。比如当对象的字段值是从外部数据中构建时,系统还没有办法对对象进行语义检查和解释,这时就适用POD来存储数据。

可能的源码实现:

// PR c++/56859
// { dg-require-effective-target c++11 }

template<unsigned size, unsigned alignment>
struct aligned_storage
{
  using type = struct { alignas(alignment) unsigned char data[size]; };
};

template <unsigned Len, unsigned Align>
struct aligned_storage
{
    using type __attribute__((aligned((Align)))) =
        char[Len];
};
// PR c++/17743

template <unsigned Len, unsigned Align>
struct aligned_storage
{
  typedef char type[Len] __attribute__((aligned((Align))));
};

示例代码:

#include <iostream>
#include <type_traits>
#include <string>
 
template<class T, std::size_t N>
class static_vector
{
    // N 个 T 的正确对齐的未初始化存储
    typename std::aligned_storage<sizeof(T), alignof(T)>::type data[N];
    std::size_t m_size = 0;
 
public:
    // 于对齐存储创建对象
    template<typename ...Args> void emplace_back(Args&&... args) 
    {
        if( m_size >= N ) // 可行的错误处理
            throw std::bad_alloc{};
        new(data+m_size) T(std::forward<Args>(args)...);
        ++m_size;
    }
 
    // 访问对齐存储中的对象
    const T& operator[](std::size_t pos) const 
    {
        // 注意: C++17 起需要 std::launder
        return *reinterpret_cast<const T*>(data+pos);
    }
 
    // 从对齐存储删除对象
    ~static_vector() 
    {
        for(std::size_t pos = 0; pos < m_size; ++pos) {
            // 注意: C++17 起需要 std::launder
            reinterpret_cast<T*>(data+pos)->~T();
        }
    }
};
 
int main()
{
    static_vector<std::string, 10> v1;
    v1.emplace_back(5, '*');
    v1.emplace_back(10, '*');
    std::cout << v1[0] << '\n' << v1[1] << '\n';
}

运行结果:

*****
**********

2. alignas

示例代码:

// sse_t 类型的每个对象将对齐到 16 字节边界
struct alignas(16) sse_t
{
  float sse_data[4];
};
 
// 数组 "cacheline" 将对齐到 128字节边界
alignas(128) char cacheline[128];
 
 
int main()
{
}

3. alignof

示例代码:

#include <iostream>
 
struct Foo {
    int   i;
    float f;
    char  c;
};
 
struct Empty {};
 
struct alignas(64) Empty64 {};
 
int main()
{
    std::cout << "Alignment of"  "\n"
        "- char             : " << alignof(char)    << "\n"
        "- pointer          : " << alignof(int*)    << "\n"
        "- class Foo        : " << alignof(Foo)     << "\n"
        "- empty class      : " << alignof(Empty)   << "\n"
        "- alignas(64) Empty: " << alignof(Empty64) << "\n";
}

运行结果:

Alignment of
- char             : 1
- pointer          : 8
- class Foo        : 4
- empty class      : 1
- alignas(64) Empty: 64

reference:

https://zh.cppreference.com/w/cpp/types/aligned_storage

https://zh.cppreference.com/w/cpp/language/alignas

https://zh.cppreference.com/w/cpp/language/alignof

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EnjoyCodingAndGame

愿我的知识,成为您的财富!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值