代码验证
#include <iostream>
#include <stdio.h>
using namespace std;
#pragma pack(8)
struct A {
short b;
int c;
char a;
};
#pragma pack()
#pragma pack(2)
struct B {
short b;
int c;
char a;
};
#pragma pack()
struct C {
char arr[7];
short b;
int c;
char a;
};
typedef struct _parent
{
short c;
char b;
int a;
}parent;
struct _child
{
char d;
是嵌套类中最大成员(int)或者实际对齐单位(还是取两者小的那个)的整数倍,\
切记,并不是子类起始变量类型short的整数倍
parent p;
int e;
};
typedef struct _parent1
{
short c;
char b;
double a;
}parent1;
struct _child1
{
int d;
是嵌套类中最大成员(double)或者实际对齐单位(还是取两者小的那个)的整数倍
parent1 p;
char e;
};
int main()
{
printf("size of struct A = %d \n", sizeof(struct A));
printf("size of struct B = %d \n", sizeof(struct B));
printf("size of struct C = %d \n", sizeof(struct C));
printf("size of struct _parent = %d \n", sizeof(_parent));
printf("size of struct _child = %d \n", sizeof(struct _child));
printf("size of struct _parent1 = %d \n", sizeof(_parent1));
printf("size of struct _child1 = %d \n", sizeof(struct _child1));
system("pause");
return 0;
}
输出结果