struct和union的大小问题
2008-01-31 17:42
274 查看
union类型以其中size最大的为其大小
struct类型以其中所有size大小之和为其大小
#include<iostream>
using namespace std;
int main()
#include<iostream>
using namespace std;
int main()
#include<iostream>
using namespace std;
int main()
#include<iostream>
using namespace std;
int main()
{
typedef struct student
{
char name[10];
char sex;
long sno;
float score [4];
} STU;
STU a[5];
cout<<sizeof(a)<<endl;
return 0;
}
答案是:160. 为什么,只是换了顺序而已呀?关键就在顺序上。
结构体中,size最大的是long,size是 4,所以,按照顺序,Char name[10];12个字节;但是这12中多分配的2个字节可以包含后面的Char sex; (问题就在这);Float score [4]; 16个字节。于是(12+4+16)×5=160
struct类型以其中所有size大小之和为其大小
#include<iostream>
using namespace std;
int main()
#include<iostream>
using namespace std;
int main()
#include<iostream>
using namespace std;
int main()
#include<iostream>
using namespace std;
int main()
{
typedef struct student
{
char name[10];
char sex;
long sno;
float score [4];
} STU;
STU a[5];
cout<<sizeof(a)<<endl;
return 0;
}
答案是:160. 为什么,只是换了顺序而已呀?关键就在顺序上。
结构体中,size最大的是long,size是 4,所以,按照顺序,Char name[10];12个字节;但是这12中多分配的2个字节可以包含后面的Char sex; (问题就在这);Float score [4]; 16个字节。于是(12+4+16)×5=160
相关文章推荐
- struct和union的大小问题
- union和struct对齐大小问题
- 关于Union,Struct and Class的大小计算问题
- 关于 Union,Struct and Class的大小计算问题的思考(转)&union和struct类型的大小计算(转)
- [C/C++基础知识] 面试再谈struct和union大小问题
- 面试再谈struct和union大小问题
- struct和union的大小
- union和struct类型的大小计算
- 学习结构体和union大小的问题
- struct和union大小的计算方法
- struct与union字节大小的终极解释
- c中union和struct类型的大小计算
- union和struct的大小计算
- struct与union内存大小计算
- sizeof struct 占内存大小问题
- 结构体和union大小的问题
- Struct和Union在内存大小上的区别
- union 和struct的sizeof问题
- struct的存储空间大小问题
- 关于struct和union存储的内存字节对齐的问题