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