Steve Bennett <[EMAIL PROTECTED]> wrote:

> > #include <stdio.h>
> > 
> > struct test1
> > {
> >     char blerg[1];
> >     char type[4];
> >     char flibble[3];
> >     char more[2];
> > } __attribute__((packed));
> > 
> > _Pragma("pack(1)") struct test2
> > {
> >     char blerg[1];
> >     char type[4];
> >     char flibble[3];
> >     char more[2];
> > };
> > 
> > int main (int argc, char **argv)
> > {
> >     printf("sizeof(test1) is %d\n", sizeof(struct test1));
> >     printf("sizeof(test2) is %d\n", sizeof(struct test2));
> >     
> >     return 0;
> > }
> > 
> > What does this print?
>
> sizeof(test1) is 10
> sizeof(test2) is 12

So _Pragma() works the same way as #pragma.


 
> > What do you get from:
> > 
> > printf("vers %d\n", __STDC_VERSION__):
>
> test2.c: In function 'main':
> test2.c:5: error: '__STDC_VERSION__' undeclared (first use in this function)
> test2.c:5: error: (Each undeclared identifier is reported only once
> test2.c:5: error: for each function it appears in.)
> test2.c:5: error: syntax error before ':' token

This is interesting! I tought that GCC is a bit closer to the standard.....


> > what do you get from:
> > 
> > struct test1
> > {
> >     char blerg[1];
> >     char type[4];
> >     char flibble[3];
> >     char more[2];
> >     char dummy[];
> > };
> > 
> > int main (int argc, char **argv)
> > {
> >     printf("sizeof(test1) is %d\n", sizeof(struct test1));
> >     
> >     return 0;
> > }
>
> sizeof(test1) is 12

This is really bad!

After reading the C-99 Standard, it turned out that the last method 
is the "official" way to prevent tail padding of structures.

Could you please run the last test again but use:

#pragma pack(1)

struct test1
{
    char blerg[1];
    char type[4];
    char flibble[3];
    char more[2];
    char dummy[];
};


Jörg

-- 
 EMail:[EMAIL PROTECTED] (home) Jörg Schilling D-13353 Berlin
       [EMAIL PROTECTED]                (uni)  
       [EMAIL PROTECTED]     (work) Blog: http://schily.blogspot.com/
 URL:  http://cdrecord.berlios.de/old/private/ ftp://ftp.berlios.de/pub/schily

Reply via email to