温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-06-26 03:48:38 来源:网络 阅读:656 作者:_xd_ 栏目:编程语言

字节对齐
结构体字节对齐:(结构体成员的数据类型为基本数据类型(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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI