Linus Torvalds wrote: > But using "array[]" means that "sizeof()" no longer works, and then you > have to use "offsetof()", which is a big pain.
This is not true under C99. If an array[] is the last member of a struct (which is what we are, AFAIK, talking about), then sizeof that struct is defined and gives the size of that struct as if the array's size were zero (but the struct cannot be used in an automatic context). Hence the idiom struct foo { ... int bar[]; }; ... struct foo *baz = malloc(sizeof *baz + 15 * sizeof (int)); ... which allocates baz in such a way that bar is a 15-element array of ints. Of course, I cannot speak of how other non-C99 compilers implement this, but GCC gets it right: [EMAIL PROTECTED]:~$ cat foo.c #include <stdio.h> struct foo { int a; int b[]; }; struct bar { int a; int b[1]; }; int main(void) { printf("sizeof(struct foo) = %zu\n" "sizeof(struct bar) = %zu\n" "sizeof(int) = %zu\n", sizeof(struct foo), sizeof(struct bar), sizeof(int)); return 0; } [EMAIL PROTECTED]:~$ gcc --std=c89 -pedantic -Wall -W -ofoo foo.c foo.c:5: warning: ISO C90 does not support flexible array members foo.c: In function 'main': foo.c:20: warning: ISO C90 does not support the 'z' printf length modifier foo.c:20: warning: ISO C90 does not support the 'z' printf length modifier foo.c:20: warning: ISO C90 does not support the 'z' printf length modifier [EMAIL PROTECTED]:~$ gcc --std=c99 -pedantic -Wall -W -ofoo foo.c [EMAIL PROTECTED]:~$ ./foo sizeof(struct foo) = 4 sizeof(struct bar) = 8 sizeof(int) = 4 [EMAIL PROTECTED]:~$ (Tested with both 3.3 and 4.0 in Debian unstable.) -- Antti-Juhani - To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [EMAIL PROTECTED] More majordomo info at http://vger.kernel.org/majordomo-info.html