中国 - 喜马拉雅

       Let life be beautiful like summer flowers . . .

1. sizeof

1.1 sizeof是一个独立的运算符,不是函数。sizeof给我们提供有关数据项目所分配的内存的大小。例如:

	cout << sizeof(long) << endl;	// 输出: 4
	cout << sizeof(double) << endl;	// 输出:8

1.2 如果将sizeof应用于一个类型,必须要像上面所示那样使用括号。但如果对一个变量使用它,可以不用括号。

	int x;
	cout << sizeof x << endl;		// 输出: 4

1.3 char类型表示单个字符,只占一个字节的内存空间,所以sizeof(char)=sizeof(‘a’)=1 。另外,相关数据项目为空,所以sizeof(‘’)不为零,而是报错。但是,“”为一个C风格的字符串,末尾会被自动加上结束符‘\n’,所以有sizeof(“”)=1,sizeof(“\0”)=2。

	char c = 'c';
	cout << sizeof(char) << endl;	// 输出:1
	cout << sizeof(c) << endl;		// 输出:1
	//cout << sizeof('') << endl;	// 编译时错误:不能为空字符常量
	cout << sizeof("") << endl;		// 输出:1
	cout << sizeof("\0") << endl;	// 输出:2

1.4 str1是一个指针,只是指向了字符串"hello"而已。所以sizeof(str1)不是字符串占的空间也不是字符数组占的空间,而是一个字符型指针占的空间。所以sizeof(str1)=sizeof(char*)=4,在C/C++中一个指针占4个字节。*str与str[0]一样,都表示字符串中的第一个字符‘h’,所以sizeof(*str1)=1。*(str1+1)表示第二个字符,依次类推。

	const char* str1 = "hello"; 
	cout << sizeof(str1) << endl;	// 输出:4
	cout << sizeof(char*) << endl;	// 输出:4
	cout << sizeof(*str1) << endl;	// 输出:1

1.5 str2是一个字符型数组。C/C++规定,对于一个数组,返回这个数组占的总空间,所以sizeof(str2)取得的是字符串"hello" 占的总空间。"hello"中,共有h e l l o \0六个字符,所以str2数组的长度是6,所以sizeof(str2)=6*sizeof(char)=6 。

	char str2[]="hello"; 
	cout << sizeof(str2) << endl;	// 输出:6

1.6 str3已经定义成了长度是8的数组,所以sizeof(str3)=8*sizeof(char)=8 。

	char str3[8]={'h',}; 
	cout << sizeof(str3) << endl;	// 输出:8

1.7 如果给出的数据项目不是字符型数组,而是一种自定义类型对像数组,那么sizeof(str3)=8*sizeof(对像类型或对像)。如:

	class A
	{
		int x;
		double d;
	};
	A a[10];
	cout << sizeof(A) << endl;		// 输出:16,此处有内存补齐,所以不是12
	cout << sizeof(a) << endl;		// 输出:160

1.8 大部分编译程序在编译的时候就把sizeof计算过了,这就是sizeof(x)可以用来定义数组长度的原因。下面数组a2的长度是根据a1的长度来定的。

	int a1[10];
	cout << sizeof(a1)/sizeof(int) << endl;		// 输出:10
	int a2[sizeof(a1)/sizeof(int)];
	cout << sizeof(a2)/sizeof(int) << endl;		// 输出:10

1.9 sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸,特别是当sizeof应用于虚参形式的数组时,得到的结果是4(指针大小) 。应牢记住数组在作为参数传递时,永远都是传递数组的地址。

#define MAXSIZE 100

// 此处等同于 void PrintSize(char* str)
void PrintSize(char str[MAXSIZE])
{
	cout << sizeof(str) << endl;		// 输出:4
}

int main()
{
	char str1[MAXSIZE];
	cout << sizeof(str1) << endl;		// 输出:100
	PrintSize(str1);
}

2.0 sizeof还可以应用于函数,注意函数fun只声明未定义:

double fun();

int main()
{
	cout << sizeof(fun()) << endl;		// 输出:8
	//cout << sizeof(fun) << endl;		// 编译时出错:非法的sizeof操作数
}

2.1 sizeof操作符不能用于函数类型,不完全类型或位字段。不完全类型指具有未知存储大小的数据类型,如未知存储大小的数组类型、未知内容的结构或联合类型、void类型等

	//cout << sizeof(void) << endl;		// 编译时出错:非法的sizeof操作数
	cout << sizeof(void*) << endl;		// 输出:4,就是指针的存储空间

总之,对于指针,sizeof操作符返回这个指针占的空间,一般是4个字节;而对于一个数组,sizeof返回这个数组所有元素占的总空间。char*与char[]容易混淆,一定要分清。sizeof可应用于任何内置类型和自定义类型。

2. strlen

2.1 strlen是函数,它不区分是数组还是指针,就读到\0为止返回长度。而且strlen是不把/0计入字符串的长度的。使用时需要包函头文件string.h。

	char str4[] = "hello";
	cout << strlen(str4) << endl;   // 输出: 5
	const char *str5 = "hello";
	cout << strlen(str5) << endl;	// 输出:5

	cout << strlen("") << endl;		// 输出:0
	cout << strlen("\0") << endl;	// 输出:0

2.2 strlen只能用于C类型字符串,注意必须是字符串且以‘\0’结尾,用于其它任何类型都是错误的。

	//cout << strlen(int) << endl;	// 出错:不能用于类型
	//cout << strlen('') << endl;	// 出错:不能用于字符
	//cout << strlen('a') << endl;	// 出错:不能用于字符
	char str6[5] = {'h', 'e', 'l', 'l', 'o'};
	//cout << strlen(str6) << endl;	// 运行时出错:str6不是一个"C风格字符串",即不是以'\0'
										// 结尾的字符串,此处输出一个不确定值,本例为19
	class B
	{
		int x;
		double d;
	};
	B b[10];
	//cout << strlen(b) << endl;	// 出错:一句话,只能用于字符串,还必须是“C风格的”

2.3 strlen的结果要在运行的时候才能计算出来。

posted on 2012-09-28 23:08  chinaxmly  阅读(869)  评论(6编辑  收藏  举报