字节对齐的的概念

字节对齐


为了使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;  
 }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值