目录
- ①结构体嵌套对齐以及结构体中含有数组的对齐
- ②其余的对齐规则
基本的结构体对齐知识点击这里
①结构体嵌套对齐以及结构体中含有数组的对齐
32位系统环境
规则一:结构体中含有数组,数组元素类型和结构中其余的最大类型取较大者 对齐
规则二:结构体S2中嵌套结构体S1,则按照S1的最大元素类型和S2中各元素类型取较大者对齐
就是max(sizeof(struct S1),sizeof(max_type_of_S2))
注释的数字表示偏移量(就是地址)
#include<iostream>
#include<vector>
struct S1 {
char a;
double b;
short c[2];
};
struct S2 {
struct S1 a;
int b;
char c;
};
int main() {
using namespace std;
cout << sizeof(S1) << " " << sizeof(S2) << endl;
}
下面代码把S1中的
#include<iostream>
#include<vector>
struct S1 {
char a;
int b;
short c;
};
struct S2 {
struct S1 a;
int b;
char c;
};
int main() {
using namespace std;
cout << sizeof(S1) << " " << sizeof(S2) << endl;
}
②其余的对齐规则
规则三:static成员放在了静态存储区,不影响sizeof结果!!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)