字节对齐和结构体的大小运算

#include<iostream>
using namespace std;

class A 
{
public:
	double dda1;
	char dda;
	int type;
};
class B 
{
  public:
	int i;
};
class C 
{
  public:
	char ch;
};
class D 
{
  public:
	int i;
	short j;
};
class E
{
  public:
	int i;
	short j;
	char ch;
};
class F 
{
  public:
    int i;
	int ii;
	short j;
	char ch;
	char chr;
};
class G 
{
  public:
	  int i;
	  int ii;
	  int iii;
	  short j;
	  char ch;
	  char chr;
};
int main(int argc, char* argv[])
{
	cout<<sizeof(A)<<endl;
    cout<<sizeof(B)<<endl;
	cout<<sizeof(C)<<endl;
    cout<<sizeof(D)<<endl;
	cout<<sizeof(E)<<endl;
    cout<<sizeof(F)<<endl;
	cout<<sizeof(G)<<endl;
	return 0;
}

/*原则*/
//结构体变量的首地址能够被其最宽的基本类型成员的大小所整除
//结构体每个成员相对于结构体首地址的偏移量都是成员大小的整数倍如果需要编译器会在成员之间加上填充字节
//结构体的总大小为结构体最宽的基本类型成员的整数倍,如果需要编译器会在最后一个成员之后加上填充字节

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值