首页 > 代码库 > union、enum、struct 内存分配问题
union、enum、struct 内存分配问题
代码和运行结果一看就懂了。
union un
{
int m;
float c;
double p;
long k;
unsigned int q;
long long g;
__int64 o;
};
enum em
{
a,b,c,d
};
struct MyStruct
{
char a;//4
float c;//4
char b;//4
double p;//8
char m1;
int k;//8
char kk;
};
struct MyStruct1
{
char a;//4
};
struct MyStruct2
{
char a;//4
float c;//4
};
#pragma pack(16)
struct MyStruct3
{
char a;//4
double c;//4
};
#pragma pack()
struct MyStruct4
{
char m;
float a;
int a1;
double c;//4
float b;
};
cout << "int=" << sizeof(int) << endl;
cout << "float=" << sizeof(float) << endl;
cout << "double=" << sizeof(double) << endl;
cout << "long=" << sizeof(long) << endl;
cout << "char=" << sizeof(char) << endl;
cout << "unsigned int=" << sizeof(unsigned int) << endl;
cout << "long long=" << sizeof(long long) << endl;
cout << "__int64=" << sizeof(long double ) << endl;
cout << "MyStruct1=" << sizeof(MyStruct1) << endl;
cout << "MyStruct2=" << sizeof(MyStruct2) << endl;
cout << "MyStruct3=" << sizeof(MyStruct3) << endl;
cout << "MyStruct4=" << sizeof(MyStruct4) << endl;
cout << sizeof(un) << endl << sizeof(em)<<endl << sizeof(MyStruct)<<endl;
union、enum、struct 内存分配问题