天天看點

轉自房秉毅的《深入了解sizeof》

本人覺得總結的比較好,就強力推薦一下。。。。

一、好首先看看sizeof和strlen在MSDN上的定義:

首先看一MSDN上如何對sizeof進行定義的:

sizeof Operator sizeof expression The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t. The expression is either an identifier or a type-cast expression (a type specifier enclosed in parentheses). When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment. When applied to a statically dimensioned array, sizeof returns the size of the entire array. The sizeof operator cannot return the size of dynamically allocated arrays or external arrays.

然後再看一下對strlen是如何定義的:

strlen Get the length of a string. Routine Required Header: strlen <string.h> size_t strlen( const char *string ); Parameter string:Null-terminated string Libraries All versions of the C run-time libraries. Return Value Each of these functions returns the number of characters in string, excluding the terminal NULL. No return value is reserved to indicate an error. Remarks Each of these functions returns the number of characters in string, not including the terminating null character. wcslen is a wide-character version of strlen; the argument of wcslen is a wide-character string. wcslen and strlen behave identically otherwise. 二、由幾個例子說開去。

第一個例子: char* ss = "0123456789"; sizeof(ss) 結果 4 ===》ss是指向字元串常量的字元指針 sizeof(*ss) 結果 1 ===》*ss是第一個字元 char ss[] = "0123456789"; sizeof(ss) 結果 11 ===》ss是數組,計算到\0位置,是以是10+1 sizeof(*ss) 結果 1 ===》*ss是第一個字元 char ss[100] = "0123456789"; sizeof(ss) 結果是100 ===》ss表示在記憶體中的大小 100×1 strlen(ss) 結果是10 ===》strlen是個函數内部實作是用一個循環計算到\0為止之前 int ss[100] = "0123456789"; sizeof(ss) 結果 400 ===》ss表示再記憶體中的大小 100×4 strlen(ss) 錯誤 ===》strlen的參數隻能是char* 且必須是以''\0''結尾的 char q[]="abc"; char p[]="a\n"; sizeof(q),sizeof(p),strlen(q),strlen(p); 結果是 4 3 3 2 第二個例子:class X { int i; int j; char k; }; X x; cout<<sizeof(X)<<endl; 結果 12 ===》記憶體補齊 cout<<sizeof(x)<<endl; 結果 12 同上 第三個例子:char szPath[MAX_PATH]   如果在函數内這樣定義,那麼sizeof(szPath)将會是MAX_PATH,但是将szPath作為虛參聲明時(void fun(char szPath[MAX_PATH])),sizeof(szPath)卻會是4(指針大小) 

三、sizeof深入了解。

  • 1.sizeof操作符的結果類型是size_t,它在頭檔案中typedef為unsigned int類型。該類型保證能容納實作所建立的最大對象的位元組大小。
  • 2.sizeof是算符,strlen是函數。
  • 3.sizeof可以用類型做參數,strlen隻能用char*做參數,且必須是以''\0''結尾的。sizeof還可以用函數做參數,比如: short f(); printf("%d\n", sizeof(f())); 輸出的結果是sizeof(short),即2。
  • 4.數組做sizeof的參數不退化,傳遞給strlen就退化為指針了。
  • 5.大部分編譯程式 在編譯的時候就把sizeof計算過了 是類型或是變量的長度這就是sizeof(x)可以用來定義數組維數的原因 char str[20]="0123456789"; int a=strlen(str); //a=10; int b=sizeof(str); //而b=20;
  • 6.strlen的結果要在運作的時候才能計算出來,時用來計算字元串的長度,不是類型占記憶體的大小。
  • 7.sizeof後如果是類型必須加括弧,如果是變量名可以不加括弧。這是因為sizeof是個操作符不是個函數。
  • 8.當适用了于一個結構類型時或變量, sizeof 傳回實際的大小, 當适用一靜态地空間數組, sizeof 歸還全部數組的尺 寸。 sizeof 操作符不能傳回動态地被分派了的數組或外部的數組的尺寸
  • 9.數組作為參數傳給函數時傳的是指針而不是數組,傳遞的是數組的首位址,如: fun(char [8]) fun(char []) 都等價于 fun(char *) 在C++裡傳遞數組永遠都是傳遞指向數組首元素的指針,編譯器不知道數組的大小如果想在函數内知道數組的大小, 需要這樣做:進入函數後用memcpy拷貝出來,長度由另一個形參傳進去 fun(unsiged char *p1, int len) { unsigned char* buf = new unsigned char[len+1] memcpy(buf, p1, len); } 有關内容見: C++ PRIMER?
  • 10.計算結構變量的大小就必須讨論資料對齊問題。為了CPU存取的速度最快(這同CPU取數操作有關,詳細的介紹可以參考一些計算機原理方面的書),C++在處理資料時經常把結構變量中的成員的大小按照4或8的倍數計算,這就叫資料對齊(data alignment)。這樣做可能會浪費一些記憶體,但理論上速度快了。當然這樣的設定會在讀寫一些别的應用程式生成的資料檔案或交換資料時帶來不便。MS VC++中的對齊設定,有時候sizeof得到的與實際不等。一般在VC++中加上#pragma pack(n)的設定即可.或者如果要按位元組存儲,而不進行資料對齊,可以在Options對話框中修改Advanced compiler頁中的Data alignment為按位元組對齊。
  • 11.sizeof操作符不能用于函數類型,不完全類型或位字段。不完全類型指具有未知存儲大小的資料類型,如未知存儲大小的數組類型、未知内容的結構或聯合類型、void類型等。如sizeof(max)若此時變量max定義為int max(),sizeof(char_v) 若此時char_v定義為char char_v [MAX]且MAX未知,sizeof(void)都不是正确形式

四、結束語

sizeof使用場合。

  • 1.sizeof操作符的一個主要用途是與存儲配置設定和I/O系統那樣的例程進行通信。例如:    void *malloc(size_t size),    size_t fread(void * ptr,size_t size,size_t nmemb,FILE * stream)。
  • 2.用它可以看看一類型的對象在記憶體中所占的單元位元組。 void * memset(void * s,int c,sizeof(s))
  • 3.在動态配置設定一對象時,可以讓系統知道要配置設定多少記憶體。
  • 4.便于一些類型的擴充,在windows中就有很多結構内型就有一個專用的字段是用來放該類型的位元組大小。
  • 5.由于操作數的位元組數在實作時可能出現變化,建議在涉及到操作數位元組大小時用sizeof來代替常量計算。
  • 6.如果操作數是函數中的數組形參或函數類型的形參,sizeof給出其指針的大小。