• C++11 内存对齐


    内存对齐alignas、alignof关键字

    目录

    内存对齐alignas、alignof关键字

    为什么要做内存对齐

    alignof和 std::alignment_of

    Abstract

    Demo

    alignas

    Abstract

    Demo

    std::aligned_storage

    Abstract

    max_align_t 和 std::align

    Abstract

    Demo


    为什么要做内存对齐

    Although memory is measured in bytes, most processors do not access memory in blocks of bytes. It accesses memory in units of double, four, eight,16, or even 32 bytes, which we refer to as memory access granularity.

    Now consider a 4-byte access granularity processor taking variables of type INT (32-bit system), which can only start reading data from memory with address multiples of 4.

    Without memory alignment, data can be stored anywhere. Now an int variable is stored in a sequence of four bytes starting at address 1. When the processor fetches the data, The first 4-byte block is read from address 0, eliminating unwanted bytes (address 0), then the next 4-byte block is read from address 4, also eliminating unwanted data (address 5,6,7), and finally the remaining two pieces of data are merged into the register. It takes a lot of work.

    Simply put, memory alignment can improve the speed at which the CPU reads data and reduce errors in accessing data (some cpus must have memory alignment, otherwise pointer access errors).

    尽管内存是以字节为单位,但是大部分处理器并不是按字节块来存取内存的.它一般会以双字节,四字节,8字节,16字节甚至32字节为单位来存取内存,我们将上述这些存取单位称为内存存取粒度.

    现在考虑4字节存取粒度的处理器取int类型变量(32位系统),该处理器只能从地址为4的倍数的内存开始读取数据。

    假如没有内存对齐机制,数据可以任意存放,现在一个int变量存放在从地址1开始的连续四个字节地址中,该处理器去取数据时,要先从0地址开始读取第一个4字节块,剔除不想要的字节(0地址),然后从地址4开始读取下一个4字节块,同样剔除不要的数据(5,6,7地址),最后留下的两块数据合并放入寄存器.这需要做很多工作.

    简单的说内存对齐能够提高 cpu 读取数据的速度,减少 cpu 访问数据的出错性(有些 cpu 必须内存对齐,否则指针访问会出错)。

    alignof和 std::alignment_of

    Abstract

    alignof用来获取内存对齐大小,alignof只能返回一个size_t而std::alignment_of继承自std::integral_constant,拥有value_type,type,value成员

    Demo

      A a;
      cout << alignof(a) << endl;
    
      cout << std::alignment_of::value << endl;   >>>> 1
      cout << std::alignment_of::value << endl;   >>>> 2

    alignas

    Abstract

    alignas可在定义变量时,改变当前变量的对齐值

    Demo

    _declspec(align(16)) class TEST {
        double a;
        double b;
        alignas(32) char c;
    };
    int main() {
        std::cout << alignof(TEST);//output: 32
        system("pause");
    }

    std::aligned_storage

    Abstract

    std::aligned_storage可以看成一个内存对齐的缓冲区

      template
     
      struct aligned_storage;

    max_align_t 和 std::align

    Abstract

    std::max_align_t用来返回当前平台的最大默认内存对齐类型,对于malloc返回的内存,其对齐和max_align_t类型的对齐大小应当是一致的

    std::align用来在一大块内存中获取一个符合指定内存要求的地址

    Demo

    std::cout << alignof(std::max_align_t) << std::endl;
    
    char buffer[] = "......";
    void *ptr = buffer;
    std::size_t space = sizeof(buffer) - 1;
    std::align(alignof(int),sizeof(char),pt,space);
  • 相关阅读:
    西北主要河流水系(绿洲)流域(山区)及高程分类数据集(一)
    【mindspore1.5.0】安装mindspore报libcuda.so没找到和libcudnn没找到
    广州小程序开发公司怎么找?
    【Java面试】Zookeeper如何实现Leader选举
    Linux docker 安装 部署
    PAT甲级 1020 Tree Traversals
    Java11改进的垃圾回收器
    中国各地级市的银行金融机构网点数据(2007-2022年)
    NVIDIA_A100_SXM2_40GB加速卡详细参数
    浦惠钱包app拉新推广渠道 实时数据
  • 原文地址:https://blog.csdn.net/qq_32378713/article/details/126278762