---
title: "C语言 sizeof "
author: "ChuanSu"
date: "2012-03-01T05:40:12Z"
tags: ["C", "sizeof", "strlen", "指针", "数组"]
categories: ["编程语言"]
slug: "c-sizeof"
draft: false
lastmod: "2012-03-07T09:46:56Z"
id: 1433003
url: "https://chuansu.iteye.com/blog/1433003"
---

1, C语言 sizeof用法以及需要特别注意的地方;
2, sizeof 与 strlen()的区别;

要想弄明白这两点其实很简单，先来看下 sizeof 的定义

> **The C Programming Language** writes:
> 
>  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].
.

> **MSDN** writes:
> 
> The sizeof operator gives the amount of storage,[color=red]in bytes[/color], required to store an object of the type of the operand.
.
C语言 基本数据类型(primary data type)有<span style="color:red">int,double,char,long</span>,可以看下面的代码来知道这些基本类型的size

```c
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(指针），可以看下面代码

```c
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，先看下面代码

```c
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所占的内存，再来看<span style="color:red">sizeof与strlen的区别</span>

> **The C Programming Language** writes:
> 
> 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]

所以char string = "Chuan Su"的所占的内存，即size为
(8+1)*1 = 9;
而 int a所占的内存，即size为 
5*4 = 20 <span style="color:blue">// ( 5*sizeof(int)= 20 bytes );</span>
如果想要得到 int a的长度，可以这样 <span style="color:red">sizeof(a)/sizeof(int)</span>
说到这里，我想请大家注意的一点是,先看下面的代码

```c
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
这里对<span style="color:red">形参</span>char string 所得到的size是 4 而非上面所说的 9，在" C专家编程 " 一书中在分析指针和数组的时候作了很好的解释

> **C专家编程** writes:
> 
> C语言允许程序员把形参声明为数组(程序员打算传递给函数的东西)或者指针(函数实际所接收到的东西)。编译器知道何时形参是作为数组声明的,但事实上在函数内部，编译器始终把它当作一个指向数组第一个元素(元素长度未知)的指针。
> 因此很有意思，没有办法把数组本身传递给一个函数，因为它总是被转换为指向数组的指针。当然，在函数内部使用指针，所能进行的对数组的操作几乎跟传递原原本本的数组没有差别。只不过，[color=red]如果想用 sizeof(实参)来获取数组的长度，所得结果不正确而已。[/color] 

所以此处所求为 指针的size而非数组的size

下面来看下<span style="color:red">sizeof和strlen()的区别</span>
二者有本质上的区别 从上面的定义可以知道sizeof只是一个operator，而strlen()则是定义一个定义在<string.h>中的函数;所以sizeof(string)是在计算string所占用的内存，包含了'\0'结尾符，strlen(string)则是用来计算字符串的长度,省略了'\0'

接下来，看下<span style="color:red">SIZE OF STRUCTURES</span>。 在 "Programming in ANSI C" 一书对此有如下解释

> **Programming in ANSI C** writes:
> 
> 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




讲了这么多，总之，对sizeof用法的理解，还是回归定义，仔细理解下文章开头的两处定义的引用，以后是不会用错的。

刚开始学C语言，先写到这里，以后有新的发现再补上。