sizeof的理解

sizeof Operator

Yields the size of its operand with respect to the size of type char.

生成sizeof中操作数的大小, 生成值与char类型的大小相关.

sizeof unary-expression
sizeof ( type-name )
Remark 

The result of the sizeof operator is of type size_t, an integral type defined in the include file STDDEF.H. This operator allows you to avoid specifying  machine-dependent data sizes in your programs.

sizeof操作符的返回值的类型是size_t,size_t是一个整数类型,它被定义在头文件STDDEF.H中.size_t这个操作符能够避免在你的程序中将与机器相关的数据大小赋予变量. 

The operand to sizeof can be one of the following:

sizeof的操作符可以是下面说明中的一种:

  • A type name. To use sizeof with a type name, the name must be enclosed in parentheses.

  • 类型名.当类型名为sizeof的操作数时,必须用括号包含类型名.

  • An expression. When used with an expression, sizeof can be specified with or without the parentheses. The expression is not evaluated.

  • 一员表达式.当一元表达式为sizeof的操作数时,括号可有可无.表达式不会被计算.

When the sizeof operator is applied to an object of type char, it yields 1. When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier. To obtain the size of the pointer represented by the array identifier, pass it as a parameter to a function that uses sizeof. For example:

当sizeof的操作数为char类型的对象时,sizeof返回1.当sizeof的操作数是一个数组名时,它返回整个数组的字节数,而不是数组名所代表的指针的大小.(数组名代表数组第一个元素的地址,也就是说数组名代表的指针类型为 数组类型 *).将数组名作为一个使用sizeof的函数的形参,就可以获得数组名代表的指针的大小.例如.

// expre_sizeof_Operator.cpp
// compile with: /EHsc
#include <iostream>

size_t getPtrSize( char *ptr )
{
   return sizeof( ptr );
}

using namespace std;
int main()
{
   char szHello[] = "Hello, world!";

   cout  << "The size of a char is: "
         << sizeof( char )
         << "\nThe length of " << szHello << " is: "
         << sizeof szHello
         << "\nThe size of the pointer is "
         << getPtrSize( szHello ) << endl;
}
The size of a char is: 1
The length of Hello, world! is: 14
The size of the pointer is 4

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

当sizeof操作符为class,struct,union类型时,返回值是该类型的对象的字节数加上填补的字节数(用来使得对象中的成员对齐到word boundaries).结果不一定是每一个成员所需的存储空间之和.编译器选项/Zp和pack pargma会影响成员的对齐边界.

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

sizeof永远不会返回0,即使操作数是一个空类.

The sizeof operator cannot be used with the following operands:

sizeof不能使用一下操作数:

  • Functions. (However, sizeof can be applied to pointers to functions.)

  • 函数.(尽管如此,函数指针可以使用)

  • Bit fields.

  • 位域s.

  • Undefined classes.

  • 没有定义的类.

  • The type void.

  • void 类型.

  • Dynamically allocated arrays.

  • 动态开辟的数组.

  • External arrays.

  • External arrays.

  • Incomplete types.

  • 不完整的类型.

  • Parenthesized names of incomplete types.

  • 不完整类型的变形.

When the sizeof operator is applied to a reference, the result is the same as if sizeof had been applied to the object itself.

If an unsized array is the last element of a structure, the sizeof operator returns the size of the structure without the array.

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

一个变量和该变量的引用的sizeof返回值相同.如何一个没有确定大小的数组是一个结构体的最后一个元素,那么sizeof返回结构体的大小而不是数组的大小.sizeof操作符常常被用来计算一个数组中元素的数量.表达式如下:

sizeof array / sizeof array[0]

转载于:https://www.cnblogs.com/yuyujiejie/archive/2011/10/28/2227994.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值