c语言sizeof

c语言的sizeof

更新:2013-07-24

//

环境gcc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2


  0)sizeof的作用


  c99:6.5.3.4:
  The sizeof operator shall not be applied to an expression that has
  function type or an incomplete type, to the parenthesized name of
  such a type, or to an expression that designates a bit-field member.
  The sizeof operator yields the size (in bytes) of its operand,
  which may be an expression or the parenthesized name of a type.

  The size is determined from the type of the operand.


  说sizeof运算符不应运算函数表达式,或者不完整类型,或者位域类型。只
  是规定对类型(表达式返回值的类型,带括号的类型名称指定的类型)所占
  的内存空间以byte为单位来计算。
  总的来说,sizoef是类型的大小计算运算符,sizeof后面跟的表达式或函数

  调用一般没有被运算,因为大多时候是在编译时便执行sizeof计算。



  1)sizeof 结构体类型或共用体类型


  c99:6.5.3.4:

  When applied to an operand that has structure or union type, the
  result is the total number of bytes in such an object, including

  internal and trailing padding.


  操作数其中若有结构体(structure)或共用体(union)时,还包括类型里

  的填充空间,即所得结果为其占内存实际大小。



  2)sizeof 数组类型


  c99:6.5.3.4:

  When applied to an operand that has array type, the result is the

  total number of bytes in the array.


  对于数组(array),sizeof 数组名,结果是数组总体成员的大小。


  提醒:注意作为函数形参声明的数组,其实际类型为指针。


  3)sizeof 函数类型。


  c99:6.3.2.4:

  A function designator is an expression that has function type.
  Except when it is the operand of the sizeof operator or the
  unary & operator, a function designator with type ‘‘function
  returning type’’ is converted to an expression that has type

  ‘‘pointer to function returning type’’.


  这里说当函数表达式被sizeof运算符和&运算符操作时,它的意义就会变成

  一个指向带有返回类型的函数的指针。


  Because this conversion does not occur, the operand of the sizeof
  operator remains a function designator and violates the constraint

  in 6.5.3.4.


  它也说到用sizeof时这个转换并不发生,6.5.3.4说是不应用sizeof去操作函

  数表达式,这里是说函数名并没有被转换成指针并调用函数吧。


  尝试了一下:
  当sizeof fun();时,相当于sizeof fun()的返回类型。
  当sizeof fun;时,值为1。
  当sizeof &fun;时,值为sizeof 指针。

  当sizeof (&fun)();时,值同于sizeof fun()。



  4)sizeof的语法


  对于操作的是表达式,sizeof后的括号是可省的,操作的是类型名称,则规

  定sizeo后的类型要被括号包围(parenthesized)。


 

  5)sizeof的返回值


  同时规定sizeof的返回值为由实现决定的size_t类型(an unsigned

  integer type),被定义在头文件<stddef.h>和其它头文件中。


本博客版权声明点击打开链接


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,sizeof是一个运算符,用于计算某个数据类型或变量在内存中所占的字节数。它可以用来计算数据类型的大小,也可以计算变量的大小。例如,sizeof(int)将返回int类型所占的字节数,sizeof(price)将返回变量price所占的字节数。在C语言中,sizeof的处理是在编译阶段完成的,即编译结束后sizeof的结果就已经确定了。 当把字符变量c放到整型数组b上面时,sizeof的结果可能会不同。这是因为在这种情况下,编译器可能会在数组b中插入一些填充字节,以满足对齐要求。因此,sizeof(test1)的结果可能会比直觉中的大小要大一些。 另外,当计算共用体变量或类型的大小时,sizeof的结果与共用体变量中宽度最大的成员的大小一致。这是因为共用体的所有成员共享同一块内存空间,只能同时存储其中一个成员的值。因此,共用体的大小取决于它最宽的成员。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C语言Sizeof()](https://blog.csdn.net/weixin_43814616/article/details/105130526)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值