您的位置:首页 > 其它

sizeof关键字

2012-12-31 10:10 169 查看
【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使用有哪些?

示例代码如下:

#include<iostream>
#include<string.h>
#include<wchar.h>
using namespace std;

struct MyNode
{
char ch2:3;                   //1+3
long ch:5;                    //4
unsigned char ch1:3;          //1+3
};

struct Node
{
unsigned char ch1:3;      //1+3
char ch2:2;
unsigned ch3:5;           //4
short st:9;               //4
int in: 23;               //4
};

class IPNode
{
int ver:4;          //4个位
int len:4;          //4个位
int sev:8;          //8个位
int total:16;       //16个位
};//4+4+8+16=32个位=4个字节

class Mail
{
char  address[30];     //地址        //30+2(对齐要求)
double zip ;           //邮政编码    //32+8=40
long int telenum ;     //电话号码    //40+8=48
};

class Information
{
char name[25] ;        //员工姓名             //25+3=28
Mail addinfo ;         //结构作为成员,嵌套  //28+48=76+4=80(对齐要求)
double salary ;        //工资                //80+8=88
};

class  Object
{
};

class   Test
{
void  fun()
{}
};

struct Inventory
{
char description[15] ;     //货物名称    //15
char  no[10] ;             //货号        //25+3=28(对齐要求)
int quantity ;             //库存数量    //28+4=32
double cost ;              //成本        //32+8=40
double retail ;            //零售价格    //40+8=48
};

struct Employee
{
char       name[27] ;       //员工姓名  27
char       address[30] ;    //家庭住址  57+3=60(对齐要求)
long int   zip ;            //邮政编码  64
long int   telenum ;        //联络电话  68+4=72(对齐要求)
double     salary ;         //工资      72+8=80
};

void main()
{
cout<<"MyNode:"<<sizeof(MyNode)<<endl;
cout<<"Node:"<<sizeof(Node)<<endl;
cout<<"IPNode:"<<sizeof(IPNode)<<endl;
cout<<"Mail:"<<sizeof(Mail)<<endl;
cout<<"Information:"<<sizeof(Information)<<endl;
cout<<"Object:"<<sizeof(Object)<<endl;
cout<<"Test:"<<sizeof(Test)<<endl;
cout<<"Inventory:"<<sizeof(Inventory)<<endl;
cout<<"Employee:"<<sizeof(Employee)<<endl;
cout<<"int:"<<sizeof(int)<<endl;
cout<<"char:"<<sizeof(char)<<endl;
cout<<"long:"<<sizeof(long)<<endl;
cout<<"double:"<<sizeof(double)<<endl;
cout<<"long double:"<<sizeof(long double)<<endl;
cout<<"float:"<<sizeof(float)<<endl;
cout<<"long int:"<<sizeof(long int)<<endl;
char ch[]="newdata";
cout<<"ch:"<<sizeof(ch)<<endl;
cout<<"ch:"<<strlen(ch)<<endl;
char *ch1="newdata";
cout<<"ch1:"<<sizeof(ch1)<<endl;
cout<<"ch1:"<<strlen(ch1)<<endl;
wchar_t ch2[]=L"newdata";
cout<<"ch2:"<<sizeof(ch2)<<endl;
cout<<"wchar_t:"<<sizeof(wchar_t)<<endl;
cout<<"unsigned short:"<<sizeof(unsigned short)<<endl;
}

/////////////////////////
/*
MyNode:12
Node:16
IPNode:4
Mail:48
Information:88
Object:1
Test:1
Inventory:48
Employee:80
int:4
char:1
long:4
double:8
long double:8
float:4
long int:4
ch:8
ch:7
ch1:4
ch1:7
ch2:16
wchar_t:2
unsigned short:2
*/


注意:对齐方式。随后我们会讨论这个话题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: