#include "stdafx.h"
struct MyStruct1
{
};
struct MyStruct2
{
char a;
};
#pragma pack(2)
struct MyStruct3
{
};
struct MyStruct4
{
char a;
};
#pragma pack()
struct MyStruct5
{
char a;
int b;
short c;
};
#pragma pack(2)
struct MyStruct6
{
char a;
int b;
short c;
};
#pragma pack()
void print()
{
int size1 = sizeof(struct MyStruct1);
int size2 = sizeof(struct MyStruct2);
int size3 = sizeof(struct MyStruct3);
int size4 = sizeof(struct MyStruct4);
int size5 = sizeof(struct MyStruct5);
int size6 = sizeof(struct MyStruct6);
printf("%d\n",size1);
printf("%d\n",size2);
printf("%d\n",size3);
printf("%d\n",size4);
printf("%d\n",size5);
printf("%d\n",size6);
}
int _tmain(int argc, _TCHAR* argv[])
{
print();
getchar();
return 0;
}
输出结果:
1
1
1
1
12
8
struct MyStruct1
{
};
struct MyStruct2
{
char a;
};
#pragma pack(2)
struct MyStruct3
{
};
struct MyStruct4
{
char a;
};
#pragma pack()
struct MyStruct5
{
char a;
int b;
short c;
};
#pragma pack(2)
struct MyStruct6
{
char a;
int b;
short c;
};
#pragma pack()
void print()
{
int size1 = sizeof(struct MyStruct1);
int size2 = sizeof(struct MyStruct2);
int size3 = sizeof(struct MyStruct3);
int size4 = sizeof(struct MyStruct4);
int size5 = sizeof(struct MyStruct5);
int size6 = sizeof(struct MyStruct6);
printf("%d\n",size1);
printf("%d\n",size2);
printf("%d\n",size3);
printf("%d\n",size4);
printf("%d\n",size5);
printf("%d\n",size6);
}
int _tmain(int argc, _TCHAR* argv[])
{
print();
getchar();
return 0;
}
输出结果:
1
1
1
1
12
8