基本语言细节--C++之sizeof操作符
来源:互联网 发布:java分子分母约分 编辑:程序博客网 时间:2024/04/28 09:54
1.首先,其不是一个函数,而是一个操作符;简单的说其作用就是返回一个对象或者类型所占的内存字节数。
2.sizeof也可以对一个函数调用求值,其结果是函数返回类型的大小,函数并不会被调用;
3.字节对齐的细节和编译器实现相关,但一般而言,满足三个准则:
Yields the size of its operand with respect to the size of type char.
sizeof unary-expression sizeof ( type-name )
sizeof unary-expression sizeof ( type-name )
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 typechar, 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 usessizeof. For example:
// 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;}
// 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: 1The length of Hello, world! is: 14The size of the pointer is 4
The size of a char is: 1The length of Hello, world! is: 14The 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 thepack 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 ifsizeof had been applied to the object itself.
If an unsized array is the last element of a structure, the sizeofoperator 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 array / sizeof array[0]
- 基本语言细节--C++之sizeof操作符
- C语言之sizeof操作符
- 基本语言细节--C++之sizeof与strlen的区别
- C语言中 sizeof 操作符使用时的一些细节 和 指针类型的大小
- 杂记之C语言函数strlen和操作符sizeof
- C语言之sizeof
- c语言之sizeof
- 程序猿之---C语言细节26(C语言中布尔类型、continue细节、sizeof举例、strlen举例)
- c语言之sizeof运算符
- C/C++ 之 sizeof 操作符总结
- 程序猿之---C语言细节24(段错误、类型提升、sizeof 'A')
- C语言基础之sizeof
- c语言之sizeof函数
- c语言 之 单链表 的 基本操作
- 对C语言中sizeof细节的三点分析
- 对C语言中sizeof细节的三点分析
- C语言基本细节(1)
- C语言中基本数据类型细节
- ignore-resource-not-found
- JavaScriptSerializer.MaxJsonLength属性问题
- TortoiseSVN权限生效一则
- Android开发之旅:应用程序基础及组件
- 日期格式化
- 基本语言细节--C++之sizeof操作符
- ERROR 1129 (HY000): Host '192.168.153.135' is blocked because of many connection errors; unblock wit
- Android开发之旅:应用程序基础及组件(续)
- CocoaPods进阶:本地包管理
- 浅谈linux性能调优之五:调优软raid
- Android开发之旅:活动与任务
- 什么是Silverlight?
- Word Break
- 摩托罗拉 开发过程中 activity listview 等样式问题