C++11特性(18):对齐方式

#include <iostream>
 
//对齐支持
//C++11标准定义alignof来查看数据的对齐方式
//Microsoft定义为__alignof来查看数据的对齐方式
struct EmptyStruct{};
class EmptyClass{};
class EmptyClass1;
struct HowManyBytes
{
     char a;
     int b;
};
 
struct ColorVector1
{
     double r;
     double g;
     double b;
     double a;
};
 
//C++11标准提供修饰符alignas设定struct/class/union的对齐方式
//Microsoft使用_declspec(align(#))来设定struct/class/union对齐方式
struct _declspec(align(32)) ColorVector2    //直接将ColorVector设定在32字节的地址边界上,其起始地址必须是32的倍数
 //等价_declspec(align(32)) struct ColorVector
{
     double r;
     double g;
     double b;
     double a;
};
 
int main()
{
     std::cout << "sizeof(EmptyStruct): " << sizeof(EmptyStruct) << std::endl;     //1
     std::cout << "sizeof(EmptyClass): " << sizeof(EmptyClass) << std::endl;    //1
     std::cout << "__alignof(EmptyClass): " << __alignof(EmptyClass) << std::endl;    //1

     //error,类型不完整,编译失败
     //std::cout << "__alignof(EmptyClass1): " << __alignof(EmptyClass1) << std::endl;//1
 
     std::cout << "sizeof(char): " << sizeof(char) << std::endl;    //1
     std::cout << "sizeof(int): " << sizeof(int) << std::endl;    //4
     std::cout << "sizeof(HowManyBytes): " << sizeof(HowManyBytes) << std::endl;    //8
     std::cout << "offset of char a: " << offsetof(HowManyBytes, a) << std::endl;    //0
     std::cout << "offset of int b: " << offsetof(HowManyBytes, b) << std::endl;    //4
 
     std::cout << "__alignof(HowManyBytes): " << __alignof(HowManyBytes) << std::endl;    //4
     std::cout << "__alignof(ColorVector1): " << __alignof(ColorVector1) << std::endl;    //8
     std::cout << "__alignof(ColorVector2): " << __alignof(ColorVector2) << std::endl;    //32
     std::cout << "sizeof(ColorVector2): " << sizeof(ColorVector2) << std::endl;    //32
 
     std::cout << "in ColorVector1 offset of double r: " << offsetof(ColorVector1, r) << std::endl;//0
     std::cout << "in ColorVector1 offset of double g: " << offsetof(ColorVector1, g) << std::endl;    //8
     std::cout << "in ColorVector1 offset of double b: " << offsetof(ColorVector1, b) << std::endl;    //16
     std::cout << "in ColorVector1 offset of double a: " << offsetof(ColorVector1, a) << std::endl;    //24
     std::cout << std::endl;

     std::cout << "in ColorVector2 offset of double r: " << offsetof(ColorVector2, r) << std::endl;//0
     std::cout << "in ColorVector2 offset of double g: " << offsetof(ColorVector2, g) << std::endl;    //8
     std::cout << "in ColorVector2 offset of double b: " << offsetof(ColorVector2, b) << std::endl;    //16
     std::cout << "in ColorVector2 offset of double a: " << offsetof(ColorVector2, a) << std::endl;    //24
 
     int a;
     long long b;
     auto &c = b;
     char d[1024] = { 0 };

     //对内置类型
     std::cout << __alignof(int) << std::endl;    //4
 
     //对变量、引用或者数组
     std::cout << __alignof(a) << std::endl    //4
          << __alignof(b) << std::endl    //8
          << __alignof(c) << std::endl    //8  引用与其引用的数据对齐值相同
          << __alignof(d) << std::endl;    //1 数组的对齐值由其元素决定
     return 0;
}

====================打个广告,欢迎关注====================

QQ:412425870
csdn博客:
http://blog.csdn.net/caychen
码云:
https://gitee.com/caychen/
github:
https://github.com/caychen

点击群号或者扫描二维码即可加入QQ群:

328243383(1群)



点击群号或者扫描二维码即可加入QQ群:

180479701(2群)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值