`
ChuanSu
  • 浏览: 52814 次
  • 性别: Icon_minigender_1
  • 来自: 石家庄
社区版块
存档分类
最新评论

C语言 sizeof

阅读更多
1, C语言 sizeof用法以及需要特别注意的地方;
2, sizeof 与 strlen()的区别;

要想弄明白这两点其实很简单,先来看下 sizeof 的定义

The C Programming Language 写道
C provides a compile-time unary operator called sizeof that can be used to compute the size of any object. The expressions
sizeof object
and sizeof(type name)
generates an integer to the size of the specified object or type in in bytes.
.
MSDN 写道
The sizeof operator gives the amount of storage,in bytes, required to store an object of the type of the operand.
.
C语言 基本数据类型(primary data type)有int,double,char,long,可以看下面的代码来知道这些基本类型的size
void test_size(void)
{
	printf("sizeof(int):%d\n",sizeof(int));
	printf("sizeof(double):%d\n",sizeof(double));
	printf("sizeof(char):%d\n",sizeof(char));
	printf("sizeof(long):%d\n",sizeof(long));	
}

sizeof(int): 4
sizeof(double): 8
sizeof(char) : 1
sizeof(long) : 4

对于pointers(指针),可以看下面代码
void pointers_size(void)
{
	char *p = "Chuan Su";
	int *p1;
	int a = 9;
	p1 = &a;
	double *p2;
	double b = 9.00;
	p2 = &b;
	char **p3;
	p3 = &p;

	printf("sizeof(p):%d\n",sizeof(p));
	printf("sizeof(p1):%d\n",sizeof(p1));
	printf("sizeof(p2):%d\n",sizeof(p2));
	printf("sizeof(p3):%d\n",sizeof(p3));
}

sizeof(p): 4
sizeof(p1): 4
sizeof(p2): 4
sizeof(p3): 4

由上面的代码可以看出,指针变量所占的内存与指针所指的对象没有任何关系,均为 4 bytes。
对于 sizeof "array" 数组的size,先看下面代码

void array_size(void)
{
	char string[] = "Chuan Su";
	int a[5];

	printf("sizeof(string):%d\n",sizeof(string));
	printf("lengthof(string):%d\n",strlen(string));
	printf("sizeof(a):%d\n",sizeof(a));
}

sizeof(string): 9
lengthof(string): 8
sizeof(a): 20

首先来看array所占的内存,再来看sizeof与strlen的区别
The C Programming Language 写道
When a string constant like "hello\n" appears in a C program, it is stored as an array of characters containing characters of the string and terminated with a '\0' to mark the end.

所以char string[] = "Chuan Su"的所占的内存,即size为
(8+1)*1 = 9;
而 int a[5]所占的内存,即size为
5*4 = 20 // ( 5*sizeof(int)= 20 bytes );
如果想要得到 int a[5]的长度,可以这样 sizeof(a)/sizeof(int)
说到这里,我想请大家注意的一点是,先看下面的代码

void array_size(char string[8])
{
	printf("sizeof(string):%d\n",sizeof(string));
	printf("lengthof(string):%d\n",strlen(string));
}

sizeof(string): 4
lengthof(string): 8
这里对形参char string[8] 所得到的size是 4 而非上面所说的 9,在" C专家编程 " 一书中在分析指针和数组的时候作了很好的解释
C专家编程 写道
C语言允许程序员把形参声明为数组(程序员打算传递给函数的东西)或者指针(函数实际所接收到的东西)。编译器知道何时形参是作为数组声明的,但事实上在函数内部,编译器始终把它当作一个指向数组第一个元素(元素长度未知)的指针。
因此很有意思,没有办法把数组本身传递给一个函数,因为它总是被转换为指向数组的指针。当然,在函数内部使用指针,所能进行的对数组的操作几乎跟传递原原本本的数组没有差别。只不过,如果想用 sizeof(实参)来获取数组的长度,所得结果不正确而已。

所以此处所求为 指针的size而非数组的size

下面来看下sizeof和strlen()的区别
二者有本质上的区别 从上面的定义可以知道sizeof只是一个operator,而strlen()则是定义一个定义在<string.h>中的函数;所以sizeof(string)是在计算string所占用的内存,包含了'\0'结尾符,strlen(string)则是用来计算字符串的长度,省略了'\0'

接下来,看下SIZE OF STRUCTURES。 在 "Programming in ANSI C" 一书对此有如下解释

Programming in ANSI C 写道
The expression sizeof(struct x)
will evaluate the number of bytes required to hold all the members of the struct x. If y is a simple structure variable of struct x,then the expression
sizeof(y) would give the same answer.However,if y is an array of variable of type struct x,then the sizeof(y)
would give the total number of bytes the array y requires.
The expression
sizeof (y) / sizeof (x)  or  sizeof (y) / sizeof y[0] would give the number of elements in the array y




讲了这么多,总之,对sizeof用法的理解,还是回归定义,仔细理解下文章开头的两处定义的引用,以后是不会用错的。

刚开始学C语言,先写到这里,以后有新的发现再补上。



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics