Steve McIntyre <[EMAIL PROTECTED]> wrote: > From the forst text from you, I asume that the problem is that the size > >of char[4] is not 4 but 16. > > No, it's not. > > >So where is the real problem? > > Quick demonstration: > > 1. gcc on an i386 box: > > smcintyre:~$ gcc -v > Reading specs from /usr/lib/gcc-lib/i486-linux/3.3.5/specs > Configured with: ../src/configure -v > --enable-languages=c,c++,java,f77,pascal,objc,ada,treelang > --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info > --with-gxx-include-dir=/usr/include/c++/3.3 --enable-shared > --enable-__cxa_atexit --with-system-zlib --enable-nls > --without-included-gettext --enable-clocale=gnu --enable-debug > --enable-java-gc=boehm --enable-java-awt=xlib --enable-objc-gc > i486-linux > Thread model: posix > gcc version 3.3.5 (Debian 1:3.3.5-13) > smcintyre:~$ uname -a > Linux smcintyre 2.6.13 #5 SMP Mon Sep 26 16:04:46 BST 2005 i686 > GNU/Linux > smcintyre:~$ ./wibble > sizeof(test1) is 10 > sizeof(test2) is 10 > > 2. gcc on an arm box: > > [EMAIL PROTECTED]:~$ gcc -v > Reading specs from /usr/lib/gcc-lib/arm-linux/3.3.5/specs > Configured with: ../src/configure -v > --enable-languages=c,c++,java,f77,pascal,objc,treelang --prefix=/usr > --mandir=/usr/share/man --infodir=/usr/share/info > --with-gxx-include-dir=/usr/include/c++/3.3 --enable-shared > --enable-__cxa_atexit --with-system-zlib --enable-nls > --without-included-gettext --enable-clocale=gnu --enable-debug > --enable-java-gc=boehm --enable-java-awt=xlib --enable-objc-gc > arm-linux > Thread model: posix > gcc version 3.3.5 (Debian 1:3.3.5-13) > [EMAIL PROTECTED]:~$ uname -a > Linux grieg 2.4.27-netwinder #1 Sun Dec 4 21:39:55 UTC 2005 armv4l > GNU/Linux > [EMAIL PROTECTED]:~$ ./wibble > sizeof(test1) is 10 > sizeof(test2) is 12 > > Both are using the following source: > > #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; > } > > As the link that I posted up-thread said, #pragma pack(1) does not > force alignment within a structure on arm, as that would clash with > the arm ABI. If you _must_ do things that way, you have to use > __attribute__((packed)). Better/more portable would be to have a > simple byte array rather than a packed structure.
I would call this default behavior of GCC a bug. I'll try to verify this with the C99 standard, but in case GCC is able to create working code at all (using __attribute__((packed))), it seems like the default behavior does not make sense. Note that countless C-programs depend on sizeof(struct something) returning useful values. 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