首页 > 其他 > 详细

字节对齐-------结构体、共用体

时间:2018-05-21 00:55:41      阅读:189      评论:0      收藏:0      [点我收藏+]
字节对齐
结构体字节对齐:(结构体成员的数据类型为基本数据类型(int,double,char,short,long等))
结构体的总大小是结构体成员中最宽基本数据类型大小的整数倍
#include<iostream>
using namespace std;
struct S0
{
int a;
char b;
short c;
};
struct S1
{
char b;
int a;
short c;
};
struct S2
{
short d;
char b;
short e;
int a;
short c;
};
void main()
{
S0 s0;
cout << sizeof(s0) << endl;
S1 s1;
cout << sizeof(s1) << endl;
S2 s2;
cout << sizeof(s2) << endl;
}
输出结果:
8
12
16

总结:对于这种成员数据类型都为基本数据类型的结构体:
1.若最宽基本数据类型的成员位置在结构体成员的边上(最上边/最下边),则其它小的数据类型字节数之和与最宽的类型大小对齐(整数倍),而结构体的大小就是最宽的数据类型大小与其它的对其后的大小(整数倍)之和;
如S0:
最宽(大)的类型为int(4字节) 其它:char(1个字节),short(2个字节) 总共3字节,与int对齐后为4字节,则结构体S0的字节大小为4+4=8 字节
2.若最宽基本数据类型的成员在结构体成员的中间位置,则是最宽的成员的上面和下面分别与最宽数据类型对齐(整数倍),则结构体的大小为3者之和(对齐后的)
如S1:
最宽(大)的类型为int(4字节) 上面:char(1字节) 对齐后占用4字节
下面:short(2字节) 对齐后占用4字节 则结构体的大小为4+4+4=12字节
如S2:
最宽(大)的类型为int(4字节) 上面:short(2字节) + char(1字节) +short(2字节)=5字节 对齐后占用8字节 (4的整数倍)
下面:short(2字节) 对齐后占用4字节 则结构体的大小为8+4+4=16字节

共用体大小:共用最宽的数据类型的空间大小
#include<iostream>
using namespace std;
union S1
{
int a;
long long b;
};

union S0
{
short b[3];
int a;
short c;
};
void main()
{
S0 s0;
cout << sizeof(s0) << endl;
S1 s1;
cout << sizeof(s1) << endl;
}
执行结果:
8
8
分析:
S1;最宽数据类型为long long(8)字节,所以共用这8个字节空间,共用体大小为8字节
S0:最宽数据类型为int(4字节),但是数组b short(2字节)*3=6字节,向上取4整数倍为8,所以共用8个字节空间

共用体里面包含结构体(或者共用体)
共用体的最大基本数据类型大于或等于其内部结构体(共用体)的在空间大小,则共用最大基本类型的字节空间
#include<iostream>
using namespace std;
struct S0
{
int a;
};
union S1
{
int a;
S0 b;
long long c;
};
void main()
{
S0 s0;
cout << sizeof(s0) << endl;
S1 s1;
cout << sizeof(s1) << endl;
}
输出:
4
8

共用体的最大基本数据类型小于其内部结构体(或者共用体)的字节空间,则共用结构体(或者共用体)字节空间
#include<iostream>
using namespace std;
struct S0
{
int a;
long long b;
};

union S1
{
int a;
S0 b;
long c;
};
void main()
{
S0 s0;
cout << sizeof(s0) << endl;
S1 s1;
cout << sizeof(s1) << endl;
}
输出:
16
16

结构体里面包含共用体(或者结构体)
结构体里面的基本数据类型没有其内部共用体(或者结构体)中的最大基本数据类型大;则遵照结构体中只有基本数据类型的规则向其内部共用体(结构体)中的最大基本数据类型字节数对齐
#include<iostream>
using namespace std;
union S1
{
int a;
};
struct S0
{
short a;
S1 cl;
char b;
};
void main()
{
S1 s0;
cout << sizeof(s0) << endl;
S0 s1;
cout << sizeof(s1) << endl;
}
输出:
4
12
#include<iostream>
using namespace std;
struct S0
{
int a;
long long b;
};
struct S1
{
int a;
S0 d;
long b;
};
void main()
{
S0 s0;
cout << sizeof(s0) << endl;
S1 s1;
cout << sizeof(s1) << endl;
}
输出:
16
32

结构体里面的基本数据类型比其内部共用体(或者结构体)中的最大基本数据类型大或者相同,则向结构体里面的最大数据类型对齐(小的加起来对齐时还需加上共用体的大小)
#include<iostream>
using namespace std;
union S1
{
int a;
};
struct S0
{
int a;
S1 cl;
long long b;
};
void main()
{
S1 s0;
cout << sizeof(s0) << endl;
S0 s1;
cout << sizeof(s1) << endl;
}
输出:
4
16
#include<iostream>
using namespace std;
struct S0
{
char a;
};
struct S1
{
S0 d;
long b;
};
void main()
{
S0 s0;
cout << sizeof(s0) << endl;
S1 s1;
cout << sizeof(s1) << endl;
}
输出:
1
8

字节对齐-------结构体、共用体

原文:http://blog.51cto.com/13665347/2118554

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!