代碼驗證
#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;
}
輸出結果