首先要明確sizeof不是函數,也不是一元運算子,他是個類似宏定義的特殊關鍵字,sizeof();括弧內在編譯過程中是不被編譯的,而是被替代類型。
如int a=8;sizeof(a);在編譯過程中,它不管a的值是什麼,只是被替換成類型sizeof(int);結果為4.
如果sizeof(a=6);呢,也是一樣的轉換成a的類型,但是要注意 因為a=6是不被編譯的,所以執行完sizeof(a=6);a的值還是8,是不變的!
記住以下幾個結論:
1.unsigned影響的只是最高位bit的意義(正負),資料長度不會被改變的。所以sizeof(unsigned int) == sizeof(int);
2.自訂類型的sizeof取值等同於它的類型原形。如typedef short WORD;sizeof(short) == sizeof(WORD)。
3.對函數使用sizeof,在編譯階段會被函數傳回值的類型取代。如:
int f1(){return 0;};
cout < <sizeof(f1()) < <endl; // f1()傳回值為int,因此被認為是int
4.只要是指標,大小就是4。如:cout < <sizeof(string*) < <endl; // 4
5.數組的大小是各維數的乘積*數組元素所屬類型的大小。如:
char a[] = "abcdef ";
int b[20] = {3, 4};
char c[2][3] = { "aa ", "bb "};
cout < <sizeof(a) < <endl; // 8
cout < <sizeof(b) < <endl; // 20*4
cout < <sizeof(c) < <endl; // 6
數組a的大小在定義時未指定,編譯時間給它分配的空間是按照初始化的值確定的,也就是8,包括末尾不顯示的‘\0’。
6.字串的sizeof和strlen的區別(見下列樣本)。
樣本:
/********************************************************** Description: siseof 操作符的作用是返回一個對象或類型名的位元組長度,它有以下三種形式:*sizeof (type name ); *sizeof ( object ); *sizeof object; ** Author:charley* DateTime:2010-12-11 22:00* Compile Environment:win7 32 位 +vs2008***********************************************************/#include <iostream>#include <string> #include <cstddef> using namespace std;int main_test5() { size_t ia; //傳回值的類型是 size_t 這是一種與機器相關的typedef定義,在cstddef標頭檔中ia = sizeof( ia ); // ok ia = sizeof ia; // ok // ia = sizeof int; // 錯誤 ia = sizeof( int ); // ok int *pi = new int[ 12 ]; cout << "pi: " << sizeof( pi ) //4個位元組:指標大小<< " *pi: " << sizeof( *pi ) //4個位元組:數組的第一個元素為int型<< endl; // 一個 string 的大小與它所指的字串的長度無關 string st1( "foobar" ); string st2( "a mighty oak" ); string *ps = &st1; cout << "st1: " << sizeof( st1 ) //32個位元組<< " st2: " << sizeof( st2 ) //32個位元組<< " ps: " << sizeof( ps ) //4個位元組:指標大小<< " *ps: " << sizeof( *ps ) //32個位元組,*ps表示字串內容,相當於sizeof(string)<< endl; cout << "short :\t" << sizeof(short) << endl; //2個位元組cout << "short* :\t" << sizeof(short*) << endl; //4個位元組cout << "short& :\t" << sizeof(short&) << endl; //2個位元組cout << "short[3] :\t" << sizeof(short[3]) << endl; // 6個位元組 = 每個元素大小2 * 共3個元素cout << "short :\t" << sizeof(int) << endl; //4個位元組cout << "short* :\t" << sizeof(unsigned int) << endl; //4個位元組char a[] = "abcdef ";int b[20] = {3, 4}; char c[2][3] = { "a", "b"}; cout <<sizeof(a) <<endl; // 8 = 8 * 1(char大小為1個位元組), 8個字元包括一個Null 字元' '和隱藏的'\0'cout <<sizeof(b) <<endl; // 80 = 20*4 int大小為4位元組cout <<sizeof(c) <<endl; // 6 = 2*3*1 char大小為1個位元組//字串的sizeof和strlen的區別char aa[] = "abcdef "; //末尾是空格char bb[20] = "abcdef "; //末尾是空格string s = "abcdef "; //末尾是空格cout <<strlen(aa) <<endl; // 7,字串長度,包括一個Null 字元' ',不包括隱藏的'\0'cout <<sizeof(aa) <<endl; // 8 = 8 * 1(char大小為1個位元組), 8個字元包括一個Null 字元' '和隱藏的'\0'cout <<strlen(bb) <<endl; // 7,字串長度 ,包括一個Null 字元' ',不包括隱藏的'\0'cout <<sizeof(bb) <<endl; // 20 = 20 * 1,字串容量 cout <<sizeof(s) <<endl; // 32 , 這裡不代表字串的長度,而是string類的大小 //錯誤: 不能將參數 1 從“std::string”轉換為“const char *”//cout <<strlen(s) <<endl; // 錯誤!s不是一個字元指標。 aa[1] = '\0 '; cout <<strlen(aa) <<endl; // 7 cout <<sizeof(aa) <<endl; // 8,sizeof是恒定的 return 0;} 一道面試題:
題目是要求輸出:TrendMicroSoftUSCN ,然後要求修改程式,使程式能輸出以上結果。代碼如下:
#include <iostream> #include <string> using namespace std; int main(int argc,char * argv[]) { string strArr1[]={ "Trend ", "Micro ", "soft "}; string *p=new string[2]; p[0]= "US "; p[1]= "CN "; cout < <sizeof(strArr1) < <endl; cout < <sizeof(p) < <endl; cout < <sizeof(string) < <endl; for(int i=0;i <sizeof(strArr1)/sizeof(string);i++) cout < <strArr1[i]; for(i=0;i <sizeof(p)/sizeof(string);i++) cout < <p[i]; cout < <endl; }
答案:
如果將
for(i=0;i <sizeof(p)/sizeof(string);i++)
改為
for(i=0;i <sizeof(p)*2/sizeof(string);i++)
答案也是不對的,sizeof(p)只是指標大小為4,要想求出數組p指向數組的成員個數,應該為
for(i=0;i <sizeof(*p)*2/sizeof(string);i++)
為什嗎?指標p指向數組,則*p就是指向數組中的成員了,成員的類型是什麼,string型,ok那麼sizeof(*p)為32,乘以2才是整個數組的大小。