摘自:http://www.vckbase.com/index.php/wv/789.html
由于最近面試碰到sizeof和strlen,被弄得糊里糊涂的,這里摘錄了一下,鞏固一下!
一、好首先看看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是數(shù)組,計算到/0位置,因此是10+1 sizeof(*ss) 結果 1 ===》*ss是第一個字符 char ss[100] = "0123456789"; sizeof(ss) 結果是100 ===》ss表示在內(nèi)存中的大小 100×1 strlen(ss) 結果是10 ===》strlen是個函數(shù)內(nèi)部實現(xiàn)是用一個循環(huán)計算到/0為止之前 int ss[100] = "0123456789"; sizeof(ss) 結果 400 ===》ss表示再內(nèi)存中的大小 100×4 strlen(ss) 錯誤 ===》strlen的參數(shù)只能是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 ===》內(nèi)存補齊 cout<<sizeof(x)<<endl; 結果 12 同上第三個例子:
1.
char
szPath[MAX_PATH]
如果在函數(shù)內(nèi)這樣定義,那么sizeof(szPath)將會是MAX_PATH,但是將szPath作為虛參聲明時(void fun(char szPath[MAX_PATH])),sizeof(szPath)卻會是4(指針大小)
三、sizeof深入理解。
1.sizeof操作符的結果類型是size_t,它在頭文件中typedef為unsigned int類型。該類型保證能容納實現(xiàn)所建立的最大對象的字節(jié)大小。
2.sizeof是算符,strlen是函數(shù)。
3.sizeof可以用類型做參數(shù),strlen只能用char*做參數(shù),且必須是以''/0''結尾的。sizeof還可以用函數(shù)做參數(shù),比如:
1.
short
f();
2.
printf
(
"%d/n"
,
sizeof
(f()));
輸出的結果是sizeof(short),即2。
4.數(shù)組做sizeof的參數(shù)不退化,傳遞給strlen就退化為指針了。
5.大部分編譯程序 在編譯的時候就把sizeof計算過了 是類型或是變量的長度這就是sizeof(x)可以用來定義數(shù)組維數(shù)的原因
1.
char
str[20]=
"0123456789"
;
2.
int
a=
strlen
(str);
//a=10;
3.
int
b=
sizeof
(str);
//而b=20;
6.strlen的結果要在運行的時候才能計算出來,時用來計算字符串的長度,不是類型占內(nèi)存的大小。
7.sizeof后如果是類型必須加括弧,如果是變量名可以不加括弧。這是因為sizeof是個操作符不是個函數(shù)。
8.當適用了于一個結構類型時或變量, sizeof 返回實際的大小, 當適用一靜態(tài)地空間數(shù)組, sizeof 歸還全部數(shù)組的尺 寸。 sizeof 操作符不能返回動態(tài)地被分派了的數(shù)組或外部的數(shù)組的尺寸
9.數(shù)組作為參數(shù)傳給函數(shù)時傳的是指針而不是數(shù)組,傳遞的是數(shù)組的首地址,如:
1.
fun(
char
[8])
2.
fun(
char
[])
都等價于 fun(char *) 在C++里傳遞數(shù)組永遠都是傳遞指向數(shù)組首元素的指針,編譯器不知道數(shù)組的大小如果想在函數(shù)內(nèi)知道數(shù)組的大小, 需要這樣做:進入函數(shù)后用memcpy拷貝出來,長度由另一個形參傳進去
1.
fun(unsiged
char
*p1,
int
len)
2.
{
3.
unsigned
char
* buf =
new
unsigned
char
[len+1]
4.
memcpy
(buf, p1, len);
5.
}
有關內(nèi)容見: C++ PRIMER?
10.計算結構變量的大小就必須討論數(shù)據(jù)對齊問題。為了CPU存取的速度最快(這同CPU取數(shù)操作有關,詳細的介紹可以參考一些計算機原理方面的書),C++在處理數(shù)據(jù)時經(jīng)常把結構變量中的成員的大小按照4或8的倍數(shù)計算,這就叫數(shù)據(jù)對齊(data alignment)。這樣做可能會浪費一些內(nèi)存,但理論上速度快了。當然這樣的設置會在讀寫一些別的應用程序生成的數(shù)據(jù)文件或交換數(shù)據(jù)時帶來不便。MS VC++中的對齊設定,有時候sizeof得到的與實際不等。一般在VC++中加上#pragma pack(n)的設定即可.或者如果要按字節(jié)存儲,而不進行數(shù)據(jù)對齊,可以在Options對話框中修改Advanced compiler頁中的Data alignment為按字節(jié)對齊。
11.sizeof操作符不能用于函數(shù)類型,不完全類型或位字段。不完全類型指具有未知存儲大小的數(shù)據(jù)類型,如未知存儲大小的數(shù)組類型、未知內(nèi)容的結構或聯(lián)合類型、void類型等。如sizeof(max)若此時變量max定義為int max(),sizeof(char_v) 若此時char_v定義為char char_v [MAX]且MAX未知,sizeof(void)都不是正確形式
四、結束語
sizeof使用場合。
1.sizeof操作符的一個主要用途是與存儲分配和I/O系統(tǒng)那樣的例程進行通信。例如:
1.
void
*
malloc
(
size_t
size),
2.
size_t
fread
(
void
* ptr,
size_t
size,
size_t
nmemb,
FILE
* stream)。
2.用它可以看看一類型的對象在內(nèi)存中所占的單元字節(jié)。
1.
void
*
memset
(
void
* s,
int
c,
sizeof
(s))
3.在動態(tài)分配一對象時,可以讓系統(tǒng)知道要分配多少內(nèi)存。
4.便于一些類型的擴充,在windows中就有很多結構內(nèi)型就有一個專用的字段是用來放該類型的字節(jié)大小。
5.由于操作數(shù)的字節(jié)數(shù)在實現(xiàn)時可能出現(xiàn)變化,建議在涉及到操作數(shù)字節(jié)大小時用sizeof來代替常量計算。
6.如果操作數(shù)是函數(shù)中的數(shù)組形參或函數(shù)類型的形參,sizeof給出其指針的大小。
新聞熱點
疑難解答