最近在論壇里總有人問關(guān)于sizeof的問題,并且本人對(duì)這個(gè)問題也一直沒有得到很好的解決,索性今天對(duì)它來個(gè)較為詳細(xì)的總結(jié),同時(shí)結(jié)合strlen進(jìn)行比較,如果能對(duì)大家有點(diǎn)點(diǎn)幫助,這是我的欣慰了。
一、好首先看看sizeof和strlen在MSDN上的定義:
首先看一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
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.二、由幾個(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ì)算到位置,因此是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ì)算到為止之前
int ss[100] = "0123456789";
sizeof(ss) 結(jié)果 400 ===》ss表示再內(nèi)存中的大小 100×4
strlen(ss) 錯(cuò)誤 ===》strlen的參數(shù)只能是char* 且必須是以''''結(jié)尾的
char q[]="abc";
char p[]="a
";
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< cout<
一、好首先看看sizeof和strlen在MSDN上的定義:
首先看一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
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.二、由幾個(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ì)算到位置,因此是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ì)算到為止之前
int ss[100] = "0123456789";
sizeof(ss) 結(jié)果 400 ===》ss表示再內(nèi)存中的大小 100×4
strlen(ss) 錯(cuò)誤 ===》strlen的參數(shù)只能是char* 且必須是以''''結(jié)尾的
char q[]="abc";
char p[]="a
";
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<