c 拾遺之資料型別占用記憶體大小總結

2021-06-21 21:09:54 字數 3871 閱讀 4413

/*************************

一:整形

int                      4位元組

long int                 4位元組

short int                2位元組

unsigned int             4位元組

unsigned long int        4位元組

unsigned short int       2位元組

二:字元型

char                       1位元組

unsigned char              1位元組

三:浮點型

float                      4位元組

double                     8位元組

long double                8位元組

unsigned long double       8位元組

unsigned double            8位元組

四:字串型

string                     16位元組

五:指標型別

所有型別的指標都是 4位元組

六:函式

除了void型。其他都函式占有的位元組數等於函式的返回型別所占有的位元組數。與函式體內部無關。

如:int fun(){}

sizeof(fun()) = 4;

int fun2()

sizeof(fun2()) = 4;

七:結構體、類

內部各資料型別占用之和,然後取比這個和值最近的4的倍數。(如果本身已經是4的倍數就不用取了)

如:struct fun

a;struct fun2

b;八:聯合體union

取其中占有位元組數最大的資料型別所占有的位元組數。

// 32位系統中c/c++中各種型別int、long、double、char表示範圍(最大最小值)

cout << "type: \t\t" << "************size**************"<< endl;

cout << "bool: \t\t" << "所佔位元組數:" << sizeof(bool);

cout << "\t最大值:" << (numeric_limits::max)();

cout << "\t\t最小值:" << (numeric_limits::min)() << endl;

cout << "char: \t\t" << "所佔位元組數:" << sizeof(char);

cout << "\t最大值:" << (numeric_limits::max)();

cout << "\t\t最小值:" << (numeric_limits::min)() << endl;

cout << "signed char: \t" << "所佔位元組數:" << sizeof(signed char);

cout << "\t最大值:" << (numeric_limits::max)();

cout << "\t\t最小值:" << (numeric_limits::min)() << endl;

cout << "unsigned char: \t" << "所佔位元組數:" << sizeof(unsigned char);

cout << "\t最大值:" << (numeric_limits::max)();

cout << "\t\t最小值:" << (numeric_limits::min)() << endl;

cout << "wchar_t: \t" << "所佔位元組數:" << sizeof(wchar_t);

cout << "\t最大值:" << (numeric_limits::max)();

cout << "\t\t最小值:" << (numeric_limits::min)() << endl;

cout << "short: \t\t" << "所佔位元組數:" << sizeof(short);

cout << "\t最大值:" << (numeric_limits::max)();

cout << "\t\t最小值:" << (numeric_limits::min)() << endl;

cout << "int: \t\t" << "所佔位元組數:" << sizeof(int);

cout << "\t最大值:" << (numeric_limits::max)();

cout << "\t最小值:" << (numeric_limits::min)() << endl;

cout << "unsigned: \t" << "所佔位元組數:" << sizeof(unsigned);

cout << "\t最大值:" << (numeric_limits::max)();

cout << "\t最小值:" << (numeric_limits::min)() << endl;

cout << "long: \t\t" << "所佔位元組數:" << sizeof(long);

cout << "\t最大值:" << (numeric_limits::max)();

cout << "\t最小值:" << (numeric_limits::min)() << endl;

cout << "unsigned long: \t" << "所佔位元組數:" << sizeof(unsigned long);

cout << "\t最大值:" << (numeric_limits::max)();

cout << "\t最小值:" << (numeric_limits::min)() << endl;

cout << "double: \t" << "所佔位元組數:" << sizeof(double);

cout << "\t最大值:" << (numeric_limits::max)();

cout << "\t最小值:" << (numeric_limits::min)() << endl;

cout << "long double: \t" << "所佔位元組數:" << sizeof(long double);

cout << "\t最大值:" << (numeric_limits::max)();

cout << "\t最小值:" << (numeric_limits::min)() << endl;

cout << "float: \t\t" << "所佔位元組數:" << sizeof(float);

cout << "\t最大值:" << (numeric_limits::max)();

cout << "\t最小值:" << (numeric_limits::min)() << endl;

cout << "size_t: \t" << "所佔位元組數:" << sizeof(size_t);

cout << "\t最大值:" << (numeric_limits::max)();

cout << "\t最小值:" << (numeric_limits::min)() << endl;

cout << "string: \t" << "所佔位元組數:" << sizeof(string) << endl;

// << "\t最大值:" << (numeric_limits::max)() << "\t最小值:" << (numeric_limits::min)() << endl;

C語言拾遺 C語言資料型別 列舉型別

摘要 變數的三個基本屬性為 作用域 鏈結屬性和儲存型別。這三個屬性決定變數的可視性和生命期。在c語言中,僅有4種基本資料型別 整型 浮點型 指標和聚合型別 如 陣列和結構體 所有其他的型別都是從這4種基本型別的某種組合派生而來。列舉是 c 語言中的一種基本資料型別,它可以讓資料更簡潔,更易讀。宣告為...

golang 常用資料型別以及占用記憶體大小

要搞清楚這個問題先要了解幾個常用的儲存單位的轉換 1.bit 位 二進位制數中的乙個數字,可以是0或者1,是計算機中資料的最小單位。二進位制的乙個 0 或乙個 1 叫一位 2.byte 位元組 計算機中資料的基本單位,每8位組成乙個位元組 int8 8位,就是乙個位元組 int16 2個位元組 in...

C語言拾遺 C語言資料型別 共用體

摘要 我們知道結構體 struct 是一種構造型別或複雜型別,它可以包含多個型別不同的成員。在c語言中,還有另外一種和結構體非常類似的語法,叫做共用體 union 共用體 union 有時也被稱為聯合或者聯合體,這也是 union 這個單詞的本意。結構體和共用體的區別在於 結構體的各個成員會占用不同...