C++各種數(shù)據(jù)類型所占內(nèi)存大小詳解
注意
基本數(shù)據(jù)類型占用數(shù)據(jù)大小還與系統(tǒng)位數(shù)有關(guān),我們假設(shè)為64位的系統(tǒng)
1.基本數(shù)據(jù)類型
- char : 1
- short: 2
- int: 4
- long long: 8
- float:4
- double:8
- bool:1
2. 指針與引用
2.1指針
指針?biāo)嫉膬?nèi)存空間不隨數(shù)據(jù)類型變化而變化
其實質(zhì)是地址空間
所以均為8
#include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <string> using namespace std; typedef struct _node{ char c1; char c2; char c3; long long i1; }node; int main(){ int i = 1; short t = 12; char c ='c'; long long ll = 521; node q; node *pn = &q; int *pi = &i; short *pt = &t; char *pc = &c; printf("%d\n",sizeof(pn)); printf("%d\n",sizeof(pi)); printf("%d\n",sizeof(pt)); printf("%d\n",sizeof(pc)); } // 8 8 8 8
2.2 引用
引用又叫別名
其所占空間與引用對象所占空間一致
#include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <string> using namespace std; typedef struct _node{ char c1; char c2; char c3; long long i1; }node; int main(){ int i = 1; short t = 12; char c ='c'; long long ll = 521; node q; node &rn = q; int &ri = i; short &rt = t; char &rc = c; printf("%d\n",sizeof(rn)); printf("%d\n",sizeof(ri)); printf("%d\n",sizeof(rt)); printf("%d\n",sizeof(rc)); } // 16 4 1 2
3.構(gòu)造類型
3.1 struct
由于struct有**邊界對齊(湊4或8)**的要求
所以盡量將同類型的寫一塊
類型小的放在最前面
關(guān)于邊界對齊舉幾個例子就好說了
類型A1的大小為4
struct p{ char c1; short s1; }A1;
神奇的是類型A2的大小也為4
但多了一個char類型變量
struct p{ char c1; char c2; short s1; }A2;
如果交換 類型A3的大小為6
struct p{ char c1; short s1; char c2; }A3;
在A1類型 基礎(chǔ)上添加一個int 變量成員
類型A4大小為8
struct p{ char c1 short s1; int i1; }A4;
在A3基礎(chǔ)上加一個int 變量成員
A5大小為 12
struct p{ char c1; short s1; char c2; int i1; }A5;
這樣似乎還不知道怎么算邊界對齊的類型大小
考慮類型A6
typedef struct _p{ char c1; char c2; char c3; }p;
這時候A6 的大小為3
說明了邊界對齊只發(fā)生在有不同數(shù)據(jù)類型時
在A6 基礎(chǔ)上加一個int類型數(shù)據(jù)得到** A7**
大小為 8
typedef struct _p{ char c1; char c2; char c3; int i1; }p;
如果A6上加上個long long 類型數(shù)據(jù)呢
A8大小為 16
typedef struct _p{ char c1; char c2; char c3; long long ll1; }p;
總結(jié)
- 當(dāng)變量中不存在8字節(jié)的變量時(不足4補充成4的倍數(shù))
- 總是向4字節(jié)對齊(多個變量)
- 出現(xiàn)的話就8字節(jié)對齊
3.2 union
這個不用說,直接取里面成員需要的最大空間
共用體u所用空間 4
union p{ char c1; short s1; char c2; int i1; }u;
3.3 enum
枚舉變量可以當(dāng)作一個int
占用4B
enum _color{ red = 1, yellow = 2, blue = 3 }Color;
3.4 class
一個空類占多少字節(jié)呢?
輸出結(jié)果為1
class p{ }; int main(){ printf("%d\n", sizeof(p)); }
給它加上一個自定義的成員函數(shù)呢
class p{ public: void pint(void){ printf("member function\n"); } };
答案還是1,那是不是加的不夠多?
class p{ public: void pint(void){ printf("member function\n"); } void pint1(void){ printf("member function1\n"); } void pint2(void){ printf("member function2\n"); } void pint3(void){ printf("member function3\n"); } };
經(jīng)過幾次實驗,類大小似乎與類的成員函數(shù)無關(guān)
可以猜測一下,一個類的對象只是將成員函數(shù)放在
類的相關(guān)位置,每個對象調(diào)用只是取這個類的位置然后調(diào)用成員函數(shù)
成員函數(shù)并不占用對象空間,所以
類的大小只取決于定義的數(shù)據(jù)類型的大小
例如
class p{ public: char c1; };
該類成員對象的大小為1
類會有邊界對齊的規(guī)則嗎?
答案是 是!
實驗一下
class p{ public: char c1; long long ll1; int t2; };
這個類(所產(chǎn)生對象)的大小為 24 !
所以類(對象)的大小相當(dāng)于把它看成結(jié)構(gòu)體的大小
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。