C语言中sizeof解析

15 篇文章 0 订阅

一、sizeof的概念

sizeof是C语言的一种单目操作符,如C语言的其他操作符++、--等。它并不是函数。sizeof操作符以字节形式给出了其操作数的存储大小。操作数可以是一个表达式或括在括号内的类型名。操作数的存储大小由操作数的类型决定。

二、sizeof的使用方法

1、用于数据类型——必须加括号

sizeof使用形式:sizeof(type)

数据类型必须用括号括住。如sizeof(int)。

2、用于变量——可加可不加括号

sizeof使用形式:sizeof(var_name)或sizeof var_name

变量名可以不用括号括住。如sizeof (var_name),sizeof var_name等都是正确形式。带括号的用法更普遍,大多数程序员采用这种形式。

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

如sizeof(max)若此时变量max定义为intmax(),sizeof(char_v) 若此时char_v定义为charchar_v [MAX]且MAX未知,sizeof(void)都不是正确形式。

三、sizeof的结果

sizeof操作符的结果类型是size_t它在头文件中typedefunsignedint类型。该类型保证能容纳实现所建立的最大对象的字节大小。

1、若操作数具有类型char、unsignedchar或signed char,其结果等于1.

ANSIC正式规定字符类型为1字节。

2、int、unsignedint 、short int、unsigned short 、long int 、unsigned long 、float、double、long double类型的sizeof 在ANSIC中没有具体规定,大小依赖于实现,一般可能分别为2、2、2、2、4、4、4、8、10.

3、当操作数是指针时,sizeof依赖于编译器。例如MicrosoftC/C++7.0中,near类指针字节数为2,far、huge类指针字节数为4.一般Unix的指针字节数为4.

4、当操作数具有数组类型时,其结果是数组的总字节数。

5、联合类型操作数的sizeof是其最大字节成员的字节数。结构类型操作数的sizeof是这种类型对象的总字节数,包括任何垫补在内。

让我们看如下结构:

strUCt{char b; double x;} a;

在某些机器上sizeof(a)=12,而一般sizeof(char)+sizeof(double)=9.

这是因为编译器在考虑对齐问题时,在结构中插入空位以控制各成员对象的地址对齐。如double类型的结构成员x要放在被4整除的地址。

6、如果操作数是函数中的数组形参或函数类型的形参,sizeof给出其指针的大小。

四、sizeof与其他操作符的关系

sizeof的优先级为2级,比/、%等3级运算符优先级高。它可以与其他操作符一起组成表达式。如i*sizeof(int);其中i为int类型变量。

五、sizeof的主要用途

1、sizeof操作符的一个主要用途是与存储分配和I/O系统那样的例程进行通信。例如:

void*malloc(size_t size),

size_tfread(void * ptr,size_t size,size_t nmemb,FILE * stream)。

2、sizeof的另一个的主要用途是计算数组中元素的个数。例如:

void* memset(void * s,int c,sizeof(s))。

六、建议

由于操作数的字节数在实现时可能出现变化,建议在涉及到操作数字节大小时用ziseof来代替常量计算。

 The result of the sizeof operator is oftype size_t, an integral type defined in the include file STDDEF.H. Thisoperator allows you to avoid specifying machine-dependent data sizes in yourprograms.   The operand to sizeof can beone of the following:   A type name. Touse sizeof with a type name, the name must be enclosed in parentheses.   An expression. When used with an expression,sizeof can be specified with or without the parentheses. The expression is notevaluated.   When the sizeof operator isapplied to an object of type char, it yields 1. When the sizeof operator isapplied to an array, it yields the total number of bytes in that array, not thesize of the pointer represented by the array identifier. To obtain the size ofthe pointer represented by the array identifier, pass it as a parameter to afunction that uses sizeof. For example: 

 翻译:

sizeof操作符的结果类型为size_t,在include文件STDDEF.H定义整型。这个操作符可以让你避免指定在你的程序依赖于计算机的数据大小。为sizeof的操作数可以是下列之一:A型名称。使用SIZEOF类型名称,名称必须括在括号中。一个表达式。当表达式中使用,SIZEOF可以指定带或不带括号。不计算表达式。 sizeof操作符应用到一个char类型的对象时,它产生1 sizeof操作符应用到一个数组时,它产生该数组的总字节数,而不是指针数组标识符代表的大小。为了获得数组标识符代表指针的大小,通过它作为一个参数传递到一个函数,使用SIZEOF。例如:

 Whenthe sizeof operator is applied to a class,struct, or union type, the result is thenumber of bytes in an object of that type, plus any padding added to alignmembers on word boundaries. The result does not necessarily correspond to thesize calculated by adding the storage requirements of the individual members.The /Zp compiler option and the pack pragma affect alignment boundaries for members.

The sizeof operator never yields 0, even for an empty class.

The sizeof operator cannot be used with the following operands:

·        Functions. (However, sizeof can beapplied to pointers to functions.)

·        Bit fields.

·        Undefined classes.

·        The type void.

·        Dynamically allocated arrays.

·        External arrays.

·        Incomplete types.

·        Parenthesized names of incomplete types.

Whenthe sizeof operator is applied to a reference, theresult is the same as if sizeof had been applied tothe object itself.

If anunsized array is the last element of a structure, the sizeof operatorreturns the size of the structure without the array.

The sizeof operator is often used to calculate the number ofelements in an array using an expression of the form:

sizeof操作符应用到类,结构或联合类型时,其结果是在该类型的对象的字节数,加任何填充添加到字边界对齐的成员。结果不一定符合加入个别成员的存储需求计算的大小。 / ZP编译器选项和包杂影响的成员对齐边界。

 sizeof运算符不会产生0,甚至为一个空类。

 sizeof操作符不能使用以下操作数:

 函数。(然而,SIZEOF可以应用到函数的指针。)

 位字段。

 未定义类。

 void类型。

 动态分配数组。

 外部数组。

 不完全类型。

 括号不完全类型的名称。

 sizeof操作符应用到引用,其结果是相同的,如果sizeof已应用于对象本身。

 如果一个可变大小的数组是结构体的最后一个元素,sizeof操作符返回没有数组的结构体的大小。

  

简单的例子

//在vs2010开发环境下 ,Win764位系统
//在vs2010开发环境下 ,Win764位系统
#include <stdio.h>
 
void main()
{
    int i=0;
    int *p = NULL;
    int a[100];
    int b[100];
    int iB,iC,iD,iE,iF,iG;
    void fun(int b[100]);
 
    //size_t            //typedef_W64 unsigned int   size_t;
    sizeof(int);        //OK 数据类型要加括号  值为4
    sizeof(i);          //OK 数据对象可加可不加括号  值为4
    //sizeof int;       //error数据类型一定要加括号
    sizeof i;           //OK 数据对象可加可不加括号  值为4
    iB=sizeof(p);       //OK 指针类型的对象  值为4
    //sizeof(int)*p;    //error数据类型一定要加括号,因为(int)*p是表达式,
                        //而sizeof是不计算表式的,而是通过表达式的最后类型来计算大小的
    iB=sizeof((int)*p); //OK   值为4
    iC=sizeof *p;       //OK   指针变量的值一般为4
    iD=sizeof (a);      //OK   数组类型的的值为4数组的长度 400
    iE=sizeof(a[100]); //请尤其注意本例。 Ok  值为4
    iF=sizeof(&a);      //OK 指针的指针类型(指针类型)的对象  值为4
    iG=sizeof(&a[0]);   //OK 指针类型的对象  值为4
    fun(b);
}
 
void fun(int b[100])  //数组传参,传进来的是数组元素类型的指针
{
    int i_A=sizeof(b);// sizeof (b) 的值是多少? 4
}


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值