[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Как это понимать



On Sun, May 01, 2005 at 11:25:53PM +0300, Dmitry Nezhevenko wrote:
> Вопрос немного не в тему рассылки но тем не менее.
> Есть простенькая прога на C:
> ==============
> #include <stdio.h>
> int main(void)
> {
>  struct oneRec
>  {
>   char b1;
>    char b2;
>   short id;
>   char lang;
>   char b3[2];
>   char b4[2];
> 
>  };
>  
>  printf("sizeof(char)=%d\n",sizeof(char));
>  printf("sizeof(short)=%d\n",sizeof(short));
>  printf("sizeof(struct oneRec)=%d\n",sizeof(struct oneRec));
> }
> 
> ==============
> У меня она выдает:
> sizeof(char)=1
> sizeof(short)=2
> sizeof(struct oneRec)=10
> 
> А теперь считаем b1, b2 - по байту, id - 2 байта, lang - 1 байт, b3 = 2 байта, 
> b4 = 2 байта. В сумме 9 байт. Но почему sizeof(struct oneRec)=10?
> 
> gcc version 3.3.5 (Debian 1:3.3.5-8)
 
jek@jekcomp:/tmp$ cat 1.c 
#include <stdio.h>
int main(void)
{
        struct oneRec
        {
                char b1;
                char b2;
                short id;
                char lang;
                char b3[2];
                char b4[2];
        } __attribute__((packed));

        printf("sizeof(char)=%d\n",sizeof(char));
        printf("sizeof(short)=%d\n",sizeof(short));
        printf("sizeof(struct oneRec)=%d\n",sizeof(struct oneRec));
}

jek@jekcomp:/tmp$ gcc 1.c 
jek@jekcomp:/tmp$ ./a.out 
sizeof(char)=1
sizeof(short)=2
sizeof(struct oneRec)=9

Дальше рассказывать? 

-- 
Best regards,
    Evgeny Khoruzhy	jek_hor@tut.by
    
A: No
Q: Should I quote below my post?



Reply to: