sizeof()运算符使用

/****************************
 
标题:sizeof 操作符应用

作者:郭臣

日期:2010.7.14
****************************/

//1、内部数据类型  
//根据编译器是16还是32位编译器确定
// cout << "unsigned short byte is "<< sizeof(unsigned short) <<endl;
// cout << "unsigned int byte is "<< sizeof(unsigned int) <<endl;
// cout << "double byte is "<< sizeof(double) <<endl;
// cout << "short byte is " << sizeof(short) <<endl;
// cout << "float byte is "<< sizeof(float) << endl;
// cout << "long byte is " << sizeof(long) <<endl;
// cout << "char byte is "<<sizeof(char) <<endl;
// cout << "int byte is "<< sizeof(int) <<endl;
// cout << "w_char byte is "<< sizeof(wchar_t) <<endl;

//2、变量
// int i;
// cout << sizeof(i) <<endl;  //变量数据类型的大小
// cout << sizeof i <<endl;  //注意这种使用方式

//3、联合
// union s
// {
// // int a;    //联合的字节取最大成员的字节,空联合为1字节 
// // char b;
// // double c;
// // long d;
// }sb;
//  cout << "union byte is " <<sizeof(sb)<<endl;

//4、结构
//struct st
//{
// double a;    //结构的字节个成员之和加额外填充字节,空结构为1字节
// char b;      
// int c;
 
//  char a;  
//  int b; 
//  double c;
//}obj;
//cout << sizeof(obj) << endl;

//5、指针
//根据编译器是16还是32位编译器确定
// char* p = "hello";
// cout << sizeof(p) << endl;

//6、数组
// char sarr[20]/*= "guochen"*/;
// cout << sizeof(sarr) << endl; //字节数是数组的长度*数组内容类型

//7、函数参数为数组,或参数为函数指针
// int a[10] = {1,2,3,5};
// Test(a, TesF);

//8、含有静态成员的类
//cout << sizeof(sobj) << endl; //类中静态成员影响类的大小,因其存储位置不同

//9 、含有虚函数的类
//cout << sizeof(sobj) << endl; //虚函数占4字节存虚表指针

//10、单层继承类, 多层继承类
//cout << sizeof(sobj) << endl; //与结构计算一样

//11、虚基类
//cout << sizeof(sobj) << endl; //加额外的虚表指针

//12、枚举
// enum en
// {
// //  EMone,
// //  EMtwo,
// //  EMthree
// }enobj;
// cout << sizeof(enobj) << endl;  //枚举占4字节,空的一样

//13、宏
// #define MAXNUM 10
// #define STRING "guochen"
// #define CHARACTER 'C'
// //宏占字节数跟定义的类型相关
// cout << sizeof(MAXNUM) <<endl;
// cout << sizeof(STRING) <<endl;
// cout << sizeof(CHARACTER) <<endl;


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

//cout << sizeof(strlen) <<endl; //非法操作
//cout << sizeof(void) <<endl; //非法操作

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值