Answers below

Joerg Schilling wrote:
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.....

Ah, well. You probably want gcc in c99 mode!
I'll try again with -std=c99

[EMAIL PROTECTED]:/share/tmp/mkisofstest$ gcc -std=c99 test2.c -o test2
[EMAIL PROTECTED]:/share/tmp/mkisofstest$ ./test2
vers 199901

Better.




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[];
};


sizeof(test1) is 12

And just in case you are wondering, I ran all those previous tests again
with -std=c99 with exactly the same results.

Cheers,
Steve


--
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to