#include<iostream>
using namespace std;
int main(int argc, char *argv[] )
{
struct //C++可以有空结构体,在G++编译器定义为1字节
{
} my_student ;
cout << "G++的空结构体大小"<< endl;
cout << sizeof(my_student) << endl;
int *p_int = NULL; //测试本机器是64位的
cout << "指针的大小:"<<endl;
cout << sizeof (p_int )<<endl;
struct student {
char name; // 1 +1
short id; // 2
char flag; // 1 总共是5
}my_student1;
//因为自身类型最大值为2 ,系统的是8 ,所以最终有效值取最小值 2
cout << "my_student1 的大小"<< endl;
cout << sizeof(my_student1)<<endl;
struct stdent1 {
char name; //1 +1
short id; // 2
int number; // 4
char str; //1 自身类型最大值是4 ,系统是8 ,故最终有效最小值为4 //一共是12
}my_student2;
cout << "my_student2 的大小"<<endl;
cout << sizeof(my_student2)<< endl;
struct teacher{
int a ; //4
char b; //1 +3 //注意:是类型对齐,将下一个结构体看成一个块,下一个结构体类型最大值为4,系统为8
struct teacher4{ // 则有效对齐值为4,所以加 +3 总为8
int id; //4
char name; //1 +1
short number; // 结构体 总共是8 ,但要自身对齐,同理,对齐有效值为4 总为8
}teacher4;
short c; // 18 //总为20 同理 自身有效值为4 //所以最终为20
}teacher1;
cout<< "teacher1 的大小"<< endl;
cout << sizeof(teacher1)<< endl;
struct teach{ //这是一道陷阱题
int a ; //4
char b; //1 +1
struct teacher4{ //里面的结构体teacher4是一个声明,不占空间
int id;
char name;
short number;
};
short c; // 总共为8 有效对齐值为4 所以最终为8
}teacher6;
cout<< "teacher6 的大小"<< endl;
cout << sizeof(teacher6)<< endl;
struct tea{
int a ; //4
char b; //1 +3
struct { //这个分析与teacher1一样 //注意struct 的标签,定义都没有
int id; //4
char name; //1 +1
short number; // 总共是8
};
short c; // 18 //20
}teacher5;
cout<< "teacher5 的大小"<< endl;
cout << sizeof(teacher5)<< endl;
struct mabo{
char str[10]; //10 +1 相当与10个char 类型,最后一个加一// 注意是类型对齐 //大小为 11
short id ; //2 //12
char str1[20]; // str1看成一个块,类似结构体的处理方法,12 + 20 = 32
int a; //4 //36
}mabo1;
cout << "mobo1 的大小"<<endl;
cout << sizeof(mabo1)<< endl;
return 0;
using namespace std;
int main(int argc, char *argv[] )
{
struct //C++可以有空结构体,在G++编译器定义为1字节
{
} my_student ;
cout << "G++的空结构体大小"<< endl;
cout << sizeof(my_student) << endl;
int *p_int = NULL; //测试本机器是64位的
cout << "指针的大小:"<<endl;
cout << sizeof (p_int )<<endl;
struct student {
char name; // 1 +1
short id; // 2
char flag; // 1 总共是5
}my_student1;
//因为自身类型最大值为2 ,系统的是8 ,所以最终有效值取最小值 2
cout << "my_student1 的大小"<< endl;
cout << sizeof(my_student1)<<endl;
struct stdent1 {
char name; //1 +1
short id; // 2
int number; // 4
char str; //1 自身类型最大值是4 ,系统是8 ,故最终有效最小值为4 //一共是12
}my_student2;
cout << "my_student2 的大小"<<endl;
cout << sizeof(my_student2)<< endl;
struct teacher{
int a ; //4
char b; //1 +3 //注意:是类型对齐,将下一个结构体看成一个块,下一个结构体类型最大值为4,系统为8
struct teacher4{ // 则有效对齐值为4,所以加 +3 总为8
int id; //4
char name; //1 +1
short number; // 结构体 总共是8 ,但要自身对齐,同理,对齐有效值为4 总为8
}teacher4;
short c; // 18 //总为20 同理 自身有效值为4 //所以最终为20
}teacher1;
cout<< "teacher1 的大小"<< endl;
cout << sizeof(teacher1)<< endl;
struct teach{ //这是一道陷阱题
int a ; //4
char b; //1 +1
struct teacher4{ //里面的结构体teacher4是一个声明,不占空间
int id;
char name;
short number;
};
short c; // 总共为8 有效对齐值为4 所以最终为8
}teacher6;
cout<< "teacher6 的大小"<< endl;
cout << sizeof(teacher6)<< endl;
struct tea{
int a ; //4
char b; //1 +3
struct { //这个分析与teacher1一样 //注意struct 的标签,定义都没有
int id; //4
char name; //1 +1
short number; // 总共是8
};
short c; // 18 //20
}teacher5;
cout<< "teacher5 的大小"<< endl;
cout << sizeof(teacher5)<< endl;
struct mabo{
char str[10]; //10 +1 相当与10个char 类型,最后一个加一// 注意是类型对齐 //大小为 11
short id ; //2 //12
char str1[20]; // str1看成一个块,类似结构体的处理方法,12 + 20 = 32
int a; //4 //36
}mabo1;
cout << "mobo1 的大小"<<endl;
cout << sizeof(mabo1)<< endl;
return 0;
}
#include<iostream>
using namespace std;
int main(int argc, char *argv[] )
{
struct //C++可以有空结构体,在G++编译器定义为1字节
{
} my_student ;
cout << "G++的空结构体大小"<< endl;
cout << sizeof(my_student) << endl;
int *p_int = NULL; //测试本机器是64位的
cout << "指针的大小:"<<endl;
cout << sizeof (p_int )<<endl;
struct student {
char name; // 1 +1
short id; // 2
char flag; // 1 总共是5
}my_student1;
//因为自身类型最大值为2 ,系统的是8 ,所以最终有效值取最小值 2
cout << "my_student1 的大小"<< endl;
cout << sizeof(my_student1)<<endl;
struct stdent1 {
char name; //1 +1
short id; // 2
int number; // 4
char str; //1 自身类型最大值是4 ,系统是8 ,故最终有效最小值为4 //一共是12
}my_student2;
cout << "my_student2 的大小"<<endl;
cout << sizeof(my_student2)<< endl;
struct teacher{
int a ; //4
char b; //1 +3 //注意:是类型对齐,将下一个结构体看成一个块,下一个结构体类型最大值为4,系统为8
struct teacher4{ // 则有效对齐值为4,所以加 +3 总为8
int id; //4
char name; //1 +1
short number; // 结构体 总共是8 ,但要自身对齐,同理,对齐有效值为4 总为8
}teacher4;
short c; // 18 //总为20 同理 自身有效值为4 //所以最终为20
}teacher1;
cout<< "teacher1 的大小"<< endl;
cout << sizeof(teacher1)<< endl;
struct teach{ //这是一道陷阱题
int a ; //4
char b; //1 +1
struct teacher4{ //里面的结构体teacher4是一个声明,不占空间
int id;
char name;
short number;
};
short c; // 总共为8 有效对齐值为4 所以最终为8
}teacher6;
cout<< "teacher6 的大小"<< endl;
cout << sizeof(teacher6)<< endl;
struct tea{
int a ; //4
char b; //1 +3
struct { //这个分析与teacher1一样 //注意struct 的标签,定义都没有
int id; //4
char name; //1 +1
short number; // 总共是8
};
short c; // 18 //20
}teacher5;
cout<< "teacher5 的大小"<< endl;
cout << sizeof(teacher5)<< endl;
struct mabo{
char str[10]; //10 +1 相当与10个char 类型,最后一个加一// 注意是类型对齐 //大小为 11
short id ; //2 //12
char str1[20]; // str1看成一个块,类似结构体的处理方法,12 + 20 = 32
int a; //4 //36
}mabo1;
cout << "mobo1 的大小"<<endl;
cout << sizeof(mabo1)<< endl;
return 0;
}