C语言 sizeof

[size=large]1, C语言 sizeof用法以及需要特别注意的地方;
2, sizeof 与 strlen()的区别;

要想弄明白这两点其实很简单,先来看下 sizeof 的定义[/size]
[quote="The C Programming Language"] C provides a compile-time unary operator called [color=black]sizeof[/color] that can be used to compute the size of any object. The expressions
[color=blue]sizeof object[/color]
and [color=blue]sizeof(type name)[/color]
generates an integer to the size of the specified object or type in [color=red]in bytes[/color].[/quote].
[quote="MSDN"]The sizeof operator gives the amount of storage,[color=red]in bytes[/color], required to store an object of the type of the operand.[/quote].
[size=medium]C语言 基本数据类型(primary data type)有[color=red]int,double,char,long[/color],可以看下面的代码来知道这些基本类型的size[/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

[size=medium]对于pointers(指针),可以看下面代码[/size]
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

[size=medium]由上面的代码可以看出,指针变量所占的内存与指针所指的对象没有任何关系,均为 4 bytes。
对于 sizeof "array" 数组的size,先看下面代码[/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

[size=medium]首先来看array所占的内存,再来看[color=red]sizeof与strlen的区别[/color][/size]
[quote="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 [color=red]terminated with a '\0' to mark the end.[/color][/quote]
[size=medium]所以char string[] = "Chuan Su"的所占的内存,即size为
(8+1)*1 = 9;
而 int a[5]所占的内存,即size为
5*4 = 20 [color=blue]// ( 5*sizeof(int)= 20 bytes );[/color]
如果想要得到 int a[5]的长度,可以这样 [color=red]sizeof(a)/sizeof(int)[/color]
说到这里,我想请大家注意的一点是,先看下面的代码[/size]
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
[size=medium]这里对[color=red]形参[/color]char string[8] 所得到的size是 4 而非上面所说的 9,在" C专家编程 " 一书中在分析指针和数组的时候作了很好的解释[/size]
[quote="C专家编程"]C语言允许程序员把形参声明为数组(程序员打算传递给函数的东西)或者指针(函数实际所接收到的东西)。编译器知道何时形参是作为数组声明的,但事实上在函数内部,编译器始终把它当作一个指向数组第一个元素(元素长度未知)的指针。
因此很有意思,没有办法把数组本身传递给一个函数,因为它总是被转换为指向数组的指针。当然,在函数内部使用指针,所能进行的对数组的操作几乎跟传递原原本本的数组没有差别。只不过,[color=red]如果想用 sizeof(实参)来获取数组的长度,所得结果不正确而已。[/color] [/quote]
[size=medium]所以此处所求为 指针的size而非数组的size

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

接下来,看下[color=red]SIZE OF STRUCTURES[/color]。 在 "Programming in ANSI C" 一书对此有如下解释[/size]
[quote="Programming in ANSI C"]The expression [color=blue]sizeof(struct x)[/color]
will evaluate [color=red] the number of bytes required to hold all the members of the struct x.[/color] If y is a simple structure variable of [color=blue]struct x[/color],then the expression
[color=blue]sizeof(y)[/color] would give the same answer.However,[color=red]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.[/color]The expression
[color=blue]sizeof (y) / sizeof (x) or sizeof (y) / sizeof y[0] [/color] would give the number of elements in the array y[/quote]


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

刚开始学C语言,先写到这里,以后有新的发现再补上。[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值