[MSDN]sizeof 操作符
来源:互联网 发布:软件开发求职简历模板 编辑:程序博客网 时间:2024/05/16 05:36
Yields the size of its operand with respect to the size of type char.
sizeof unary-expression
sizeof ( type-name )
Remarks
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.
The operand to sizeof can be one of the following:
A type name. To use 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 not evaluated.
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:
Example
Copy Code
// 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;
}
Sample Output
Copy Code
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.
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 be applied to pointers to functions.)
Bit fields.
Undefined classes.
The type void.
Dynamically allocated 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:
Copy Code
sizeof array / sizeof array[0]
- [MSDN]sizeof 操作符
- sizeof 操作符
- sizeof 操作符详解
- sizeof 操作符
- sizeof操作符
- 操作符sizeof
- sizeof操作符
- sizeof操作符
- sizeof 操作符
- sizeof操作符
- sizeof操作符
- 详解sizeof操作符
- 详解sizeof操作符
- sizeof 操作符
- sizeof 操作符详解
- 解读sizeof操作符
- 再论sizeof操作符
- sizeof操作符
- 七个受用一生的心理寓言 【转】
- EventUtil.js
- 怎样把SQL_SERVER数据库里的(类型是image)图片显示在aspx页面里的image控件里(转http://dev.csdn.net/article/45/45934.shtm)
- 用java完成C#同样的DES对称加密
- asp.net利用多线程执行长时间的任务,客户端显示出任务的执行进度的示例(二)
- [MSDN]sizeof 操作符
- 有关C++迭代器
- 开机自动启动VMware并启动虚拟系统
- 有关上传图片缩略图保存到文件夹得问题
- 嵌入式系统启动信息的分析(个人解释标记********)
- Java中Native关键字的作用
- 在.NET中调用Oracle9i存储过程经验总结
- 【html效果】使文字来回移动,当鼠标放上去停止
- google真的要走?