字节对齐
为了使CPU能够对变量进行快速的访问,变量的起始地址应该具有某些特性,即所谓的“对齐”,比如4字节的int型,其起始地址应该位于4字节的边界上,即起始地址能够被4整除,也即“对齐”跟数据在内存中的位置有关。
1、自然对齐:
如果一个变量的内存地址正好位于它长度的整数倍,他就被称做自然对齐。
2、非自然对齐
比如在32位cpu下,假设一个整型变量的地址为0x00000004(为4的倍数),那它就是自然对齐的,而如果其地址为0x00000002(非4的倍数)则是非对齐的。
-
原则1:占用的内存空间大小需要是结构体中占用最大内存空间的类型的整数倍。
union example { int a[5]; //20 char b; //1 double c; //8 }; int result = sizeof(example); //24字节 ———— 需满足 double 类型整数倍
struct example { int a[5]; //20 char b; //24 double c; //32 }test_struct; int result = sizeof(test_struct); //32字节
struct example { char b; //8 double c; //16 int a; //20 }test_struct; int result = sizeof(test_struct); //24字节———— 还需满足 double 类型整数倍
3、强制对齐
#pragma pack(n) //表示结构体成员所占用内存的起始地址需要是n的整数倍
-
原则二:对齐字节数 = min(成员起始地址应是n的倍数时填充的字节数, 自然对齐时填充的字节数)。
#include <stdio.h> #include <stdlib.h> #include <iostream> #pragma pack(4) //表示占用内存的起始地址需要是4的整数倍 using namespace std; struct example{ char a; //1 double b; //12 int c; //16 }test_struct; int main() { int result = sizeof(test_struct); cout << result << endl; //16字节 system("Pause"); return 0; }
#pragma pack(4) using namespace std; struct example{ char a; //1 double b; //12 char c; //13 }test_struct; int result = sizeof(test_struct); cout << result << endl; //16字节 总体字节也需要是结构体中占用最大内存空间的类型的整数倍
#pragma pack(4) using namespace std; struct example{ int a; //4 char b; //6 short int c; //8 0x05不能整除2字节,所以前面char b需要扩充一个字节 char d; //9 }test_struct; int result = sizeof(test_struct); cout << result << endl; //12字节
#pragma pack(8) using namespace std; struct example{ int a; //4 char b; //6 short int c; //8 int d; //12 }test_struct; int result = sizeof(test_struct); cout << result << endl; //12
-
规则一:对齐字节数 = min(成员起始地址应是n的倍数时填充的字节数, 自然对齐时填充的字节数)。
-
规则二:同时满足占用的内存空间大小需要是结构体中占用最大内存空间的类型的整数倍。(当与规则一冲突时,优先考虑规则一)
4、取消字节对齐
定义结构体时加上 attribute((packed))
#include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; struct { char b; double c; int a; }__attribute__((packed)) test_struct; //取消字节对齐 int main() { int result = sizeof(test_struct); cout << result << endl; return 0; }