sizeoof分析




很多时候会碰到sizeof(struct A)=?这样的问题,不管是考试,还是面试,领导们都喜欢出这样的问题,今天决定详细讨论一下;
先看下面的例子:
         
struct a
{
    char no[10];
    int p;
    long int pp;
    unsigned int ppp;
    char x;
    float y;
    double h;
}xy;
struct BF1
{
char f1 : 3;
char f2 : 4;
char f3 : 5;
};
struct BF2
{
char f1 : 3;
short f2 : 4;
char f3 : 5;
};
struct b
{
    char i;
    short j:2;
    long k:1;
};
struct B{
    int a;
    double b;
    char c[100];
    long d;
};
typedef struct Test
{
    short  a;
    char   b;
    double c;
    char   d;
    char   e;
    char   f;
    char   g;
}sTest;
void main()
{
    printf("char no:     %d /n",sizeof(xy.no));
    printf("int p:       %d /n",sizeof(xy.p));   
    printf("long int pp: %d /n",sizeof(xy.pp));
    printf("unsigned ppp:%d /n",sizeof(xy.ppp));
    printf("char x:      %d /n",sizeof(xy.x));
    printf("float y:     %d /n",sizeof(xy.y));
    printf("double h:    %d /n",sizeof(xy.h));
    printf("struct a:    %d /n",sizeof(struct a)); 
    printf("size of bf1 %d/n",sizeof(struct BF1));
    printf("size of bf2 %d/n",sizeof(struct BF2));
    printf("sizeof b is %d/n",sizeof(struct b));
    printf("sizeof B is %d/n",sizeof(struct B));
    printf("sizeof stest is %d/n",sizeof(sTest));

对结构体里面的变量的sizeof值,不会有对齐的问题,该是多少就是多少。
所以:
   printf("char no:     %d /n",sizeof(xy.no));
    printf("int p:       %d /n",sizeof(xy.p));   
    printf("long int pp: %d /n",sizeof(xy.pp));
    printf("unsigned ppp:%d /n",sizeof(xy.ppp));
    printf("char x:      %d /n",sizeof(xy.x));
    printf("float y:     %d /n",sizeof(xy.y));
    printf("double h:    %d /n",sizeof(xy.h));
的答案就是:
char no:     10 
int p:       4 
long int pp: 8 
unsigned ppp:4 
char x:      1 
float y:     4 
double h:    8 
这里需要注意的是sizeof(long)的值,我的机子是8,所以所有的值都是以8来算的,如果你的机子值为4,那么下面的有关long的答案都需要作相应的修改。
再看
printf("struct a:    %d /n",sizeof(struct a)); 
这里就比较有意思了:
看看它的内容是这样的,
struct a
{
    char no[10];
    int p;
    long int pp;
    unsigned int ppp;
    char x;
    float y;
    double h;
}xy;
它的值=10+2(对齐)+4+8+4+1+3(对齐)+4+8+4(对齐)=48
括号里面这些对齐的值怎么来的呢?
1,因为int p占用四个字节,而no变量占用10个字节,10并不是4的倍数,所以要加2才能除以4;
2,float x占用4个字节,而float x之前占用了29个字节,29加3才可以除以4,所以要加3;
3,如果不加最后的4,那么一共是44个字节,44必须等被这个结构体里面的最大的数据类型的空间对齐,
最大是long int和double都等于8,所以44+4才可以除以8,于是最后的值就是48了。

有了上面的解释,我想其它的就都可以解释了;
有关位域的留在后面讲,先看类似的巩固一下上面的解释:
struct B{
    int a;
    double b;
    char c[100];
    long d;
};
sizeof(struct B)=?
sizeof(struct B)=4+4(对齐)+8+100+4(对齐)+8=128;
算对了吗?
再来一个:
typedef struct Test
{
    short  a;
    char   b;
    double c;
    char   d;
    char   e;
    char   f;
    char   g;
}sTest;
sizeof(sTest)=?
sizeof(sTest)=2+1+5(对齐)+8+1+1+1+1+4(对齐)=24;
算对了吗?
下面看看有关位域的:
struct BF1
{
char f1 : 3;
char f2 : 4;
char f3 : 5;
};
sizeof(struct BF1)=3+4+5=12bits
大于1个字节小于2个字节,所以只能是分配2个字节了;
struct BF2
{
char f1 : 3;
short f2 : 4;
char f3 : 5;
};
sizeof(struct BF2)=3+4+5=12bits
最后还是2个字节,注意这个值在不同的平台不一样,在VC上等于4个字节;
struct b
{
    char i;
    short j:2;
    long k:1;
};
这个呢?
sizeof(struct b)=8+2+1=11bits
也就是2个字节,但是需要和long对齐,所以结果就是8了;
最后来看看有关几个字符串的:
char *str="0123456";
sizeof(str)=?  
sizeof(*str)=?
strlen(str)=?
char str1[]="0123456";
sizeof(str1)=?
sizeof(*str1)=?
strlen(str1)=?
char str2[10]="0123456";
sizeof(str2)=?
sizeof(*str2)=?
strlen(str2)=?
sizeof(str)=8;sizeof(str1)=8;sizeof(str2)=10;
sizeof(*str)=1;sizeof(*str1)=1;sizeof(*str2)=1;
strlen(str)=7;strlen(str1)=7;strlen(str2)=7;
OK了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值