struct和union的大小问题
生活随笔
收集整理的這篇文章主要介紹了
struct和union的大小问题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
union類型以其中size最大的為其大小
struct類型以其中所有size大小之和為其大小 #include<iostream>
using?namespace?std;
int?main()
{
????typedef?union?{long?i;?int?k[5];?char?c;}?DATE;
????struct?data?{?int?cat;?DATE?cow;?double?dog;}?too;
????DATE?max;
????cout<<"sizeof(struct?date)+sizeof(max)?=?"<<sizeof(too)+sizeof(max)<<endl;
????cout<<"sizeof(too)?=?"<<sizeof(too)<<endl;
????cout<<"sizeof(max)?=?"<<sizeof(max)<<endl;
????cout<<"struct?data.cow?size?=?"<<sizeof(too.cow)<<endl;
????cout<<"union?DATE.i?size?=?"<<sizeof(max.i)<<endl;
????cout<<"union?char.c?size?=?"<<sizeof(max.c)<<endl;
} sizeof(struct date)+sizeof(max)返回52
#include<iostream>
using?namespace?std;
int?main()
{
????typedef?union?student?
{
???????char?name[10];
???????long?sno;?
???????char?sex;?
???????float?score?[4];?
}?STU;?
STU?a[5];
cout<<sizeof(a)<<endl;
return?0;
}? 初始化了一個含有5個UNION的數(shù)組,由于UNION以其中最大的元素float作為大小? 16*5=80
#include<iostream>
using?namespace?std;
int?main()
{
????typedef?struct?student?
{
???????char?name[10];
???????long?sno;?
???????char?sex;?
???????float?score?[4];?
}?STU;?
STU?a[5];
cout<<sizeof(a)<<endl;
return?0;
}? 輸出為180
自然對齊(natural alignment)即默認(rèn)對齊方式,是指按結(jié)構(gòu)體的成員中(類型)size最大的成員作為基本的分配單元,而且與其順序有這密切的聯(lián)系。size最大的是long,size是 4,所以,按照順序,Char name[10];12個字節(jié);Long sno; 4個字節(jié);Char sex; 4個字節(jié)(這里對齊了);Float score [4]; 16個字節(jié)。于是(12+4+4+16)×5=180
#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;
}?
struct類型以其中所有size大小之和為其大小 #include<iostream>
using?namespace?std;
int?main()
{
????typedef?union?{long?i;?int?k[5];?char?c;}?DATE;
????struct?data?{?int?cat;?DATE?cow;?double?dog;}?too;
????DATE?max;
????cout<<"sizeof(struct?date)+sizeof(max)?=?"<<sizeof(too)+sizeof(max)<<endl;
????cout<<"sizeof(too)?=?"<<sizeof(too)<<endl;
????cout<<"sizeof(max)?=?"<<sizeof(max)<<endl;
????cout<<"struct?data.cow?size?=?"<<sizeof(too.cow)<<endl;
????cout<<"union?DATE.i?size?=?"<<sizeof(max.i)<<endl;
????cout<<"union?char.c?size?=?"<<sizeof(max.c)<<endl;
} sizeof(struct date)+sizeof(max)返回52
#include<iostream>
using?namespace?std;
int?main()
{
????typedef?union?student?
{
???????char?name[10];
???????long?sno;?
???????char?sex;?
???????float?score?[4];?
}?STU;?
STU?a[5];
cout<<sizeof(a)<<endl;
return?0;
}? 初始化了一個含有5個UNION的數(shù)組,由于UNION以其中最大的元素float作為大小? 16*5=80
#include<iostream>
using?namespace?std;
int?main()
{
????typedef?struct?student?
{
???????char?name[10];
???????long?sno;?
???????char?sex;?
???????float?score?[4];?
}?STU;?
STU?a[5];
cout<<sizeof(a)<<endl;
return?0;
}? 輸出為180
自然對齊(natural alignment)即默認(rèn)對齊方式,是指按結(jié)構(gòu)體的成員中(類型)size最大的成員作為基本的分配單元,而且與其順序有這密切的聯(lián)系。size最大的是long,size是 4,所以,按照順序,Char name[10];12個字節(jié);Long sno; 4個字節(jié);Char sex; 4個字節(jié)(這里對齊了);Float score [4]; 16個字節(jié)。于是(12+4+4+16)×5=180
#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. 為什么,只是換了順序而已呀?關(guān)鍵就在順序上。
結(jié)構(gòu)體中,size最大的是long,size是 4,所以,按照順序,Char name[10];12個字節(jié);但是這12中多分配的2個字節(jié)可以包含后面的Char sex; (問題就在這);Float score [4]; 16個字節(jié)。于是(12+4+16)×5=160
轉(zhuǎn)載于:https://www.cnblogs.com/cobain/archive/2008/01/31/1060271.html
總結(jié)
以上是生活随笔為你收集整理的struct和union的大小问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一吨纸多少钱啊?
- 下一篇: 如何在OSCOMMERCE中安装中文语言