C++ over-aligned memory allocation

#include <cstdio>
#include <cstdlib>
#include <new>
struct S{
    char buff[10];
};
 
int main()
{
    S* p1 = new S;
    std::printf("default-aligned address:   %p\n", static_cast<void*>(p1));
 
    S* p2 = new (std::align_val_t{32}) S;
    std::printf("over-aligned address:   %p\n", static_cast<void*>(p2));
}

new运算符支持指定按什么对齐了。over-aligned意思是超过默认的对齐的尺寸。std::max_align_t是个类型,其对齐大小为标量当中最大的那个

#include <new>
#include <iostream>
int main()
{
   std::cout << alignof(std::max_align_t) << '\n';
}

经测试,MS上的值为8。其它平台的典型值为8或16。

参考:https://ask.helplib.com/others/post_12363048

http://en.cppreference.com/w/cpp/memory/new/operator_new

转载于:https://www.cnblogs.com/thomas76/p/8618175.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值