sizeof关键字

【1】sizeof是关键字还是函数呢?

答案:关键字

为什么说是关键字呢?看看下面分析:

我们借助编译器确认它的身份。看下面的例子。示例选择题如下:

int i = 10;
A:sizeof(int)
B:sizeof(i)
C:sizeof int
D:sizeof i

A,B不用说,32位系统下值为4。而D也通过编译,并且值也是4,没有括号竟然都可以通过?充分说明sizeof不是函数。

那C呢?编译器报错误了!!如果按关键字理解的话,后面的括号是可以省略的呀?那我们再想想sizeof int 表示什么啊?

我们记住int前面可以添加unsigned 或  可以添加 const等关键字而不能添加sizeof。好,总结如下:

<1>sizeof是一个关键字

<2>计算变量空间大小时括号可以省略

<3>计算类型(模子)空间大小时括号不可以省略

<4>32位系统之下,所有指针变量占4个字节。

【2】有关sieof使用有哪些?

示例代码如下:

  1 #include<iostream>
  2 #include<string.h>
  3 #include<wchar.h>
  4 using namespace std;
  5 
  6 struct MyNode
  7 {
  8     char ch2:3;                   //1+3
  9     long ch:5;                    //4
 10     unsigned char ch1:3;          //1+3
 11 };
 12 
 13 struct Node
 14 {
 15     unsigned char ch1:3;      //1+3  
 16     char ch2:2;               
 17     unsigned ch3:5;           //4
 18     short st:9;               //4
 19     int in: 23;               //4
 20 };
 21 
 22 class IPNode
 23 {
 24     int ver:4;          //4个位
 25     int len:4;          //4个位
 26     int sev:8;          //8个位
 27     int total:16;       //16个位
 28 };//4+4+8+16=32个位=4个字节
 29 
 30 class Mail
 31 {
 32     char  address[30];     //地址        //30+2(对齐要求)
 33     double zip ;           //邮政编码    //32+8=40
 34     long int telenum ;     //电话号码    //40+8=48
 35 };
 36  
 37 class Information
 38 {
 39     char name[25] ;        //员工姓名             //25+3=28
 40     Mail addinfo ;         //结构作为成员,嵌套  //28+48=76+4=80(对齐要求)
 41     double salary ;        //工资                //80+8=88
 42 }; 
 43 
 44 class  Object
 45 {
 46 };
 47 
 48 class   Test
 49 {
 50     void  fun()
 51     {}
 52 };
 53 
 54 struct Inventory
 55 {
 56     char description[15] ;     //货物名称    //15
 57     char  no[10] ;             //货号        //25+3=28(对齐要求)
 58     int quantity ;             //库存数量    //28+4=32
 59     double cost ;              //成本        //32+8=40
 60     double retail ;            //零售价格    //40+8=48
 61 };
 62 
 63 struct Employee
 64 {
 65     char       name[27] ;       //员工姓名  27
 66     char       address[30] ;    //家庭住址  57+3=60(对齐要求)
 67     long int   zip ;            //邮政编码  64
 68     long int   telenum ;        //联络电话  68+4=72(对齐要求)  
 69     double     salary ;         //工资      72+8=80
 70 };
 71 
 72 void main()
 73 {
 74     cout<<"MyNode:"<<sizeof(MyNode)<<endl;
 75     cout<<"Node:"<<sizeof(Node)<<endl;
 76     cout<<"IPNode:"<<sizeof(IPNode)<<endl;
 77     cout<<"Mail:"<<sizeof(Mail)<<endl;
 78     cout<<"Information:"<<sizeof(Information)<<endl;
 79     cout<<"Object:"<<sizeof(Object)<<endl;
 80     cout<<"Test:"<<sizeof(Test)<<endl;
 81     cout<<"Inventory:"<<sizeof(Inventory)<<endl;
 82     cout<<"Employee:"<<sizeof(Employee)<<endl;
 83     cout<<"int:"<<sizeof(int)<<endl;
 84     cout<<"char:"<<sizeof(char)<<endl;
 85     cout<<"long:"<<sizeof(long)<<endl;
 86     cout<<"double:"<<sizeof(double)<<endl;
 87     cout<<"long double:"<<sizeof(long double)<<endl;
 88     cout<<"float:"<<sizeof(float)<<endl;
 89     cout<<"long int:"<<sizeof(long int)<<endl;
 90     char ch[]="newdata";
 91     cout<<"ch:"<<sizeof(ch)<<endl;
 92     cout<<"ch:"<<strlen(ch)<<endl;
 93     char *ch1="newdata";
 94     cout<<"ch1:"<<sizeof(ch1)<<endl;
 95     cout<<"ch1:"<<strlen(ch1)<<endl;
 96     wchar_t ch2[]=L"newdata";
 97     cout<<"ch2:"<<sizeof(ch2)<<endl;
 98     cout<<"wchar_t:"<<sizeof(wchar_t)<<endl;
 99     cout<<"unsigned short:"<<sizeof(unsigned short)<<endl;
100 }
101 
102 /////////////////////////
103 /*
104 MyNode:12
105 Node:16
106 IPNode:4
107 Mail:48
108 Information:88
109 Object:1
110 Test:1
111 Inventory:48
112 Employee:80
113 int:4
114 char:1
115 long:4
116 double:8
117 long double:8
118 float:4
119 long int:4
120 ch:8
121 ch:7
122 ch1:4
123 ch1:7
124 ch2:16
125 wchar_t:2
126 unsigned short:2
127 */ 

注意:对齐方式。随后我们会讨论这个话题。

 

posted @ 2012-12-31 10:10  kaizenly  阅读(1317)  评论(0编辑  收藏  举报
打赏